﻿Imports IDServer
Imports System
Imports System.IO
Imports System.String
Imports System.Data
Imports System.Data.SqlClient
Imports UserDataGridView
Imports System.Threading



Public Class Frm_SALPOS



    Implements IDServer.IDServer.IDServer

    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataset As DataSet
    Private MoneyTypeDS As DataSet
    Private mTableA As DataTable
    Private F1Table As DataTable
    Private F2Table As DataTable
    Private F3Table As DataTable
    Private F4Table As DataTable
    Private mTableD As DataTable
    Private DataTable As DataTable
    Private MarkDs As DataTable
    Private M_Ds As DataSet
    Private DSMember As DataSet
    Private GifDataSet As DataSet
    Private DSSalesPromotion As DataSet
    Private CaptionStr As String = "序號^GoodsCode&貨品編號^Code&貨品名稱^CnName&&數量^mCount&金額^SaleMoney&條碼^BarCode&顔色^ColorCName"
    Private x, y As Integer
    Private IsPutUpA As Boolean = True
    Private IsPutUpB As Boolean = True
    Private IsOutPut As Boolean
    Private ManagePassword As String
    Private GoodsSalePrice As Double
    Private Frm_PosManage As New Frm_PosManage
    Private WithEvents MainForm As Frm_MainForm

    ''掛單時保存會員信息
    Private MemberHash As New Hashtable



    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property



#Region "TabControlPage1"

#Region "interface define"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region


#Region "sub or function"

    ''' <summary>
    ''' 設置控件的ReadOnly屬性
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetEnable()
        Me.txtBarCode.Enabled = ActionEnable
        Me.txtSumMoney.ReadOnly = True
        'Me.txtmMoney.Enabled = ActionEnable
        Me.txtLitMoney.ReadOnly = True
        Me.txtMemberCode.Enabled = ActionEnable
        Me.txtCount.Enabled = ActionEnable
        'Me.txtmMoney.Enabled = ActionEnable
        Me.TextBox1.Enabled = ActionEnable
        Me.ComboBox1.Enabled = ActionEnable
        Me.BtnConfimSale.Enabled = ActionEnable
        Me.TextBox2.Enabled = ActionEnable
    End Sub

    ''' <summary>
    ''' 綁定網格
    ''' </summary>
    ''' <param name="mIndex"></param>
    ''' <remarks></remarks>
    Private Sub DataBindGridA(Optional ByVal mIndex As Integer = 0)
        Try
            Dim Str() As String = CaptionStr.Split("&")
            mTableA = New DataTable
            With mTableA
                .Columns.Add("GoodsCode", GetType(String))
                .Columns.Add("Code", GetType(String))
                .Columns.Add("CnName", GetType(String))
                .Columns.Add("mCount", GetType(Decimal))
                .Columns.Add("SaleMoney", GetType(Decimal))
                .Columns.Add("BarCode", GetType(String))
                .Columns.Add("ColorCName", GetType(String))
            End With
            Me.DataGridView1.DataSource = mTableA
            With Me.DataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHash(0).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                    If .Columns(i).Name = "mCount" Then
                        .Columns(i).ReadOnly = False
                        .Columns(i).DefaultCellStyle.BackColor = Color.LightYellow
                    Else
                        .Columns(i).ReadOnly = True
                        .Columns(i).DefaultCellStyle.BackColor = Color.LightGray
                    End If
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                .Columns("mCount").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Columns("SaleMoney").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            Me.DataGridView1.Columns("BarCode").Visible = False
            Me.DataGridView1.Columns("ColorCName").Visible = False
        Catch ex As Exception
            MsgBox("DataBindGridA:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindPayMoneyTypeGrid()

        Try
            Dim nTable As New DataTable
            With nTable
                .Columns.Add("ListCode", GetType(String))
                .Columns.Add("ListA", GetType(String))
                .Columns.Add("MoneyCount", GetType(Decimal))
                .Columns.Add("CARNO", GetType(String))
            End With
            MoneyTypeDS = New DataSet
            MoneyTypeDS.Tables.Add(nTable)
            Me.UserDataGridView1.DataSource = MoneyTypeDS.Tables(0)
            With Me.UserDataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHash(1).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .Columns(1).ReadOnly = True
                .RowHeadersWidth = 10
                .Columns(0).Visible = False
                .Columns(0).Width = 20
                .Columns(1).Width = 80
                .Columns(2).Width = 100
                .Columns(3).Width = 100
                .Columns(1).DefaultCellStyle.BackColor = Color.LightGray
                .Columns(1).DefaultCellStyle.ForeColor = Color.Blue
                .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .ImeMode = Windows.Forms.ImeMode.Disable
            End With
            LoadPayMoneyType()
        Catch ex As Exception
            MsgBox("BindPayMoneyTypeGrid:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadPayMoneyType()

        Try
            Dim sql As String
            sql = "select ListCode,ListA,MoneyCount=0.00,CarNo='' from dbo.BSC_OtherDefineB where ItemNo=41  order by ListA "
            MoneyTypeDS = ReturnDataset(sql)
            Me.UserDataGridView1.DataSource = MoneyTypeDS.Tables(0)
        Catch ex As Exception
            MsgBox("LoadPayMoneyType:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 綁定HashTable
    ''' </summary>
    ''' <param name="mIndex"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function BindHash(Optional ByVal mIndex As Integer = 0) As Hashtable
        Try
            Dim mHash As New Hashtable
            With mHash
                Select Case mIndex
                    Case 0
                        .Add("GoodsCode", "序號")
                        .Add("Code", "貨品編號")
                        .Add("CnName", "貨品名稱")
                        .Add("mCount", "數量")
                        .Add("SaleMoney", "小計金額")
                    Case 1
                        .Add("ListCode", "貨幣代碼")
                        .Add("ListA", "付款類型")
                        .Add("MoneyCount", "金額")
                        .Add("CARNO", "卡號")
                    Case 2
                        .Add("SaleOrdeNo", "銷售單號")
                        .Add("GoodsBarCode", "貨品條碼")
                        .Add("GoodsCode", "貨品編碼")
                        .Add("GoodsName", "貨品名稱")
                        .Add("GoodsColor", "貨品顔色")
                        .Add("GoodsSize", "貨品規格")
                        .Add("GoodsCount", "貨品數量")
                        .Add("SaleMoney", "總金額")
                        .Add("BuyDate", "購買日期")
                        .Add("SalePrice", "SalePrice")
                End Select
            End With
            Return mHash
        Catch ex As Exception
            MsgBox("BindHash:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' 加載貨品資料
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadGoods()
        Try
            Dim sql As String = ""
            ''如查少了列則在這裏加不能加PIC列,或者a.* 查image速度太慢
            'sql = "select a.*,b.ColorCName from BSC_Goods a left join BSC_ColorDefine b on a.Color=b.ColorCode;"
            sql &= " SELECT [Barcode],[Code],[Color],[Spec],[Shortname],[Cnname],[Enname],[SupperItem]"
            sql &= " ,[GoodsType],[MinPrice],[PurhasePrice],[Price],[SalePrice],[status],b.ColorCName"
            sql &= "  FROM [dbo].[BSC_Goods] a"
            sql &= "  left join BSC_ColorDefine b on a.Color=b.ColorCode where a.status=1"

            mDataset = New DataSet
            mDataset = ReturnDataset(sql)
        Catch ex As Exception
            MsgBox("LoadGoods:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
    ''' <summary>
    ''' 加載打印設置
    ''' </summary>
    ''' <remarks></remarks>
    Private Function PrintSet() As String
        Try
            Dim sql As String = ""
            Dim DSset As DataSet
            '' Dim DTtable As DataTable
            sql = "select b.listcode from BSC_OtherDefineA a,BSC_OtherDefineB b where a.id=b.itemno and a.itemcode='Posset'"
            DSset = New DataSet
            Dim vol As String = Exec(sql, E_ExecType.ExecuteScalar).ToString
            Return vol
        Catch ex As Exception

        End Try
    End Function

    ''' <summary>
    ''' 加載會員和積分定義
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadMemberPerson()
        Try
            Dim ServerDate As String = ReturnServerDate()
            Dim sql As String = ""
            sql += "select a.ID,a.Code,a.Cnname,a.LinkMan,a.CnAddress,a.FType,a.Phone1,a.FLevel,a.FIntegral,b.ListA,b.ListB as Rat from BSC_Customer a left join "
            sql += "(select b.* from dbo.BSC_OtherDefineA a inner join BSC_OtherDefineB b on a.ID=b.ItemNo where a.ItemCode='MemberLevel') b "
            sql += "on a.FLevel=b.ListCode where a.Status=1 and a.FType='Retail'and convert(nvarchar(10),a.stopdate,21)>='" & ServerDate & "';"
            sql += "select a.*,b.* from BSC_OtherDefineA a inner join BSC_OtherDefineB b on a.ID=b.ItemNo where a.ItemCode='MemberDefine'"
            DSMember = New DataSet
            DSMember = ReturnDataset(sql)
        Catch ex As Exception
            MsgBox("LoadMemberPerson:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 加載促銷設置
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadSalesPromotionSet()
        Try
            Dim sql As String = ""
            'Dim mDate As String = ReturnServerDate()
            sql = "select a.Num,Convert(varchar(10),a.StartDate,21) as StartDate,Convert(varchar(10),a.EndDate,21) as EndDate,a.PromotionType,a.IsSame,a.Dept,a.DisCount as DisCountA,a.Reduce as ReduceA,a.PurQty as PurQtyA,a.Present as PresentA,b.SN,b.BarCode,b.DisCount as DisCountB,b.Reduce as ReduceB,b.PurQty as PurQtyB,b.Present as PresentB,b.GoodsType"
            sql += " from dbo.PUR_PromotionA a inner join PUR_PromotionB b on a.Num=b.Num where  Convert(varchar(10),Getdate(),21)  between Convert(varchar(10),a.Startdate,21) and convert(varchar(10),a.EndDate,21)    and charindex('," & PubCompanyCode & ",',','+Dept+',')>0 "
            'sql = "select a.Num,Convert(varchar(10),a.StartDate,21) as StartDate,Convert(varchar(10),a.EndDate,21) as EndDate,a.PromotionType,a.IsSame,a.Dept,a.DisCount as DisCountA,a.Reduce as ReduceA,a.PurQty as PurQtyA,a.Present as PresentA from PUR_PromotionA a where '" & mDate & "' between Convert(varchar(10),a.Startdate,21) and convert(varchar(10),a.EndDate,21) ;"
            'sql += " select b.Num,b.SN,b.BarCode,b.DisCount as DisCountB,b.Reduce as ReduceB,b.PurQty as PurQtyB,b.Present as PresentB,b.GoodsType from PUR_PromotionB b"
            DSSalesPromotion = New DataSet
            DSSalesPromotion = ReturnDataset(sql)
        Catch ex As Exception
            MsgBox("LoadSalesPromotionSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 加載銷售人員
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadSaleMan()
        Try
            Dim sql As String = ""
            Dim SaleManDs As New DataSet
            sql = "select  distinct  fCode,fCName from dbo.BSC_Employee  where Isnull(fCName,'')<>''"
            SaleManDs = ReturnDataset(sql)
            Me.ComboBox1.ValueMember = "fCode"
            Me.ComboBox1.DisplayMember = "fCName"
            Me.ComboBox1.DataSource = SaleManDs.Tables(0)
        Catch ex As Exception
            MsgBox("LoadSaleMan:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' Load Gif Set
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadGifSet()
        Dim sql As String
        Try
            sql = "select * from dbo.BSC_Gift where Convert(varchar(10),getdate(),121) between Convert(varchar(10),StartDate,21) and Convert(varchar(10),EndDate,21) and Status=0"
            GifDataSet = ReturnDataset(sql)
        Catch ex As Exception
            MsgBox("LoadGifSet:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 會員自動升級
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateMemberMark()
        Dim sql As String = ""
        Try
            Dim Rates As Double = 1.0 ''積分

            If DSMember.Tables(1).Rows.Count > 0 Then
                Dim ConsumeIndex As String = DSMember.Tables(1).Rows(0).Item("Listcode") & ""
                Dim Mark As Double = Convert.ToDouble(DSMember.Tables(1).Rows(0).Item("ListB"))
                If ConsumeIndex.Length > 0 Then
                    If ConsumeIndex = "0" Then
                        Dim ConSumeTime As Double = Convert.ToDouble(DSMember.Tables(1).Rows(0).Item("ListA"))
                        Rates = Mark / ConSumeTime
                        sql = "Update BSC_Customer Set FIntegral= FIntegral+ " & Rates & " where Code='" & Me.TextBox3.Text.Replace("*", "") & "'"
                        ReturnObject(sql)
                    ElseIf ConsumeIndex = "1" Then
                        Dim ConsumeMoney As Double = Val(DSMember.Tables(1).Rows(0).Item("ListA"))
                        If Val(Me.txtSumMoney.Text) >= Val(ConsumeMoney) Then
                            Rates = (Mark / ConsumeMoney) * Val(Me.txtSumMoney.Text)
                            sql = "Update BSC_Customer Set FIntegral= FIntegral +" & Rates & " where Code='" & Me.TextBox3.Text.Replace("*", "") & "'"
                            ReturnObject(sql)
                        End If
                    End If
                End If
            End If
            sql = "select * from BSC_Customer where Code='" & Me.TextBox3.Text.Replace("*", "") & "' ;"
            sql += "select * from BSC_OtherDefineB where ItemNo=17 "
            Dim Ms_Data As New DataSet
            Dim ddcode As String = ""

            Dim fLevel As String = 1
            Ms_Data = ReturnDataset(sql)
            If Not Ms_Data Is Nothing Then
                Dim fMark As Double = Convert.ToDouble(Ms_Data.Tables(0).Rows(0).Item("FIntegral"))
                fLevel = Ms_Data.Tables(0).Rows(0).Item("fLevel") & ""
                For i As Integer = 0 To Ms_Data.Tables(1).Rows.Count - 1
                    Dim Listcode As String = Ms_Data.Tables(1).Rows(i).Item("Listcode") & ""
                    If i = Ms_Data.Tables(1).Rows.Count - 1 Then
                        If fMark >= Convert.ToDouble(Ms_Data.Tables(1).Rows(i).Item("ListC")) Then
                            sql = "Update BSC_Customer Set FLevel='" & Listcode & "' where Code='" & Me.TextBox3.Text & "'"
                            ReturnObject(sql)
                            ddcode = Listcode
                        End If
                    Else
                        If fMark >= Convert.ToDouble(Ms_Data.Tables(1).Rows(i).Item("ListC")) And fMark < Convert.ToDouble(Ms_Data.Tables(1).Rows(i + 1).Item("ListC")) Then
                            sql = "Update BSC_Customer Set FLevel='" & Listcode & "' where Code='" & Me.TextBox3.Text & "'"
                            ReturnObject(sql)
                            ddcode = Listcode
                        End If
                    End If
                Next
            End If

            ''更新會員數據集
            Dim srow() As DataRow = DSMember.Tables(0).Select("code='" & Me.TextBox3.Text.Trim.Replace("*", "") & "'")
            If srow.Length > 0 Then
                srow(0).Item("FIntegral") = Val(srow(0).Item("FIntegral")) + Val(Rates) ''積分
                'If Ms_Data.Tables(1).Rows.Count > 0 Then

                '    Dim ListB As Double = Val(Ms_Data.Tables(1).Rows(1).Item("ListB"))
                '    srow(0).Item("Rat") = ListB ''折扣
                'End If
                Dim ddrow() As DataRow = Ms_Data.Tables(1).Select("Listcode='" & ddcode & "'")
                If ddrow.Length > 0 Then
                    srow(0).Item("Rat") = Val(ddrow(0).Item("ListB")) ''折扣
                End If
            End If
        Catch ex As Exception
            MsgBox("UpdateMemberMark:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 挂單事件
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub PutSaleOrdeUp()
        Try

            Dim mPoint As Point
            Dim mSize As New Size(75, 23)
            Dim X, Y As Integer
            Dim mIndex As String = ""
            Dim mTag As String = ""
            Dim nTag As String = ""
            Dim mButton As Button
            Dim mCount As Integer = Me.GrpBoxUp.Controls.Count

            If mCount >= 4 Then
                MsgBox("挂單數已達到最大,不能繼續挂單!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtBarCode.Focus()
                Exit Sub
            End If

            If mTableA.Rows.Count <= 0 Then
                MsgBox("沒有可挂起的單據!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtBarCode.Focus()
                Exit Sub
            End If

            If Me.GrpBoxUp.Controls.Count > 0 Then
                For i As Integer = 0 To Me.GrpBoxUp.Controls.Count - 1
                    mTag += Me.GrpBoxUp.Controls(i).Tag + ","
                Next
            End If

            If mTag.IndexOf("1") = -1 Then
                X = 17
                Y = 20
                F1Table = mTableA.Copy
                nTag = "1"
            ElseIf mTag.IndexOf("2") = -1 Then
                X = 17
                Y = 60
                F2Table = mTableA.Copy
                nTag = "2"
            ElseIf mTag.IndexOf("3") = -1 Then
                X = 17
                Y = 100
                F3Table = mTableA.Copy
                nTag = "3"
            ElseIf mTag.IndexOf("4") = -1 Then
                X = 17
                Y = 140
                F4Table = mTableA.Copy
                nTag = "4"
            End If

            mPoint.X = X
            mPoint.Y = Y
            mButton = New Button
            mButton.Location = mPoint
            mButton.Size = mSize
            mButton.Tag = nTag
            mButton.Text = "挂單" & "(" & nTag & ")"
            Me.GrpBoxUp.Controls.Add(mButton)
            Me.txtBarCode.Text = ""
            Me.txtLitMoney.Text = "0.00"
            Me.txtMemberCode.Text = ""
            'Me.txtmMoney.Text = "0.00"
            Me.txtSumMoney.Text = "0.00"
            Me.txtlevemoney.Text = "0.00"
            mTableA.Rows.Clear()
            mTableA.AcceptChanges()
            Me.DataGridView1.DataSource = mTableA

            ''會員
            Me.MemberHash(nTag) = Me.TextBox3.Text.Trim.Replace("*", "")
            Me.TextBox3.Text = ""
            Me.TextBox4.Text = ""
            Me.TextBox5.Text = ""
            Me.TextBox6.Text = ""
            Me.TextBox7.Text = ""
            Me.txtMemberCode.Focus()
        Catch ex As Exception
            MsgBox("PutSaleOrdeUp:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' Filter-----GoodsBar
    ''' </summary>
    ''' <param name="WhereStr"></param>
    ''' <remarks></remarks>
    Private Sub FindBarCode(ByVal WhereStr As String)
        Try
            Dim sql As String = ""
            Dim BarCodeDs As New DataSet
            sql = "select * from BSC_Goods where BarCode like '%*" & WhereStr & "%'"
            BarCodeDs = ReturnDataset(sql)
            'Me.CBGoods.ValueMember = "BarCode"
            'Me.CBGoods.DisplayMember = "ShortName"
            'Me.CBGoods.DataSource = BarCodeDs.Tables(0)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' Update Gif----Status
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function UpdateGifNum() As Boolean
        ' Dim sql As String
        Try
            'sql = "Update dbo.BSC_Gift set Status=1 where Num= "
            'ReturnObject(sql)
            'Return True
        Catch ex As Exception
            MsgBox("UpdateGifNum:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub FillMemberInfo(ByVal tag As String)
        Dim code As String = ""
        If Me.MemberHash.ContainsKey(tag) Then
            code = Me.MemberHash(tag)
            Me.MemberHash.Remove(tag)
        End If

        Dim Rows() As DataRow = DSMember.Tables(0).Select("Code='" & code & "'")
        If Rows.Length > 0 Then
            For Each mRow As DataRow In Rows
                Me.TextBox3.Text = mRow("Code").ToString.Replace("*", "")
                Me.TextBox4.Text = mRow("Cnname") & ""
                Me.TextBox5.Text = mRow("ListA") & ""
                Me.TextBox6.Text = mRow("Rat") & ""
                Me.TextBox7.Text = mRow("FIntegral") & ""
            Next
        Else
            Me.TextBox3.Text = ""
            Me.TextBox4.Text = ""
            Me.TextBox5.Text = ""
            Me.TextBox6.Text = ""
            Me.TextBox7.Text = ""
        End If

    End Sub

    ''' <summary>
    ''' 返回主貨品編碼
    ''' </summary>
    ''' <param name="BarCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetMasterBarCode(ByVal BarCode As String)
        Dim MasterBarCode As String = ""

        Dim sql As String = " select Replace(BarCode,'*','') as BarCode from BSC_BarCode where Replace(SuppBarCode,'*','')='" & BarCode & "' "

        MasterBarCode = Exec(sql, E_ExecType.ExecuteScalar) & ""

        Return MasterBarCode
    End Function

    ''' <summary>
    '''  掃描貨品錄入
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BarCodeScanInput()
        Try

            ''查找條碼 多種情況下的條碼
            Dim fQuanty As Integer = Val(txtCount.Text) ''數量
            If fQuanty <= 0 Then
                fQuanty = 1
            End If

            Dim BarCode As String = Me.txtBarCode.Text.Trim
            Dim Rows() As DataRow = mDataset.Tables(0).Select("BarCode='*" & BarCode & "*'")

            If Rows.Length <= 0 Then
                '' 是否是次條碼 , 查找主條碼
                Dim MasterBarCode As String = GetMasterBarCode(BarCode)
                Rows = mDataset.Tables(0).Select("BarCode='*" & MasterBarCode & "*'")
                If Rows.Length > 0 Then '' 查找成功，使用主條碼
                    Me.txtBarCode.Text = MasterBarCode
                    BarCode = MasterBarCode
                Else
                    '' 是否是套裝優費
                    '' 因為套裝不是使用的貨品條碼而是使用促銷編號做條碼
                    SuitPromotion(BarCode)
                End If
            End If

            ''條碼不存在
            If Rows.Length <= 0 Then Exit Sub


            ''一)查找貨品當前銷售價格
            Dim fGoodsSalePrice As Double = Val(Rows(0).Item("SalePrice"))
            Dim currentSalePrice As Double = GetCurrentSalefPrice(BarCode)
            If currentSalePrice > 0 Then
                fGoodsSalePrice = currentSalePrice
            End If


            ''二)叛斷是否有促銷價

            Dim PromotionRows() As DataRow = DSSalesPromotion.Tables(0).Select("BarCode='*" & BarCode & "*'  and  PromotionType<>'套裝優惠'   and GoodsType<>'贈送品'", "Num desc") ''套裝優惠 單獨處理  贈送品不能算是促銷

            If PromotionRows.Length > 0 Then ''同一貨品在同一時間最好只有一種促銷方案

                Dim Num As String = PromotionRows(0).Item("Num") & ""
                Dim PromotionType As String = PromotionRows(0).Item("PromotionType") & "" ''方案類型
                Dim IsSame As Boolean = CType(PromotionRows(0).Item("IsSame"), Boolean) ''統一折扣
                'Dim Dept As String = PromotionRows(0).Item("Dept") & "" ''門店
                Dim DiscountA As Double = Val(PromotionRows(0).Item("DiscountA") & "") * 0.01 ''折扣率(%)
                Dim ReduceA As Double = Val(PromotionRows(0).Item("ReduceA") & "") ''折扣金額
                Dim PurQtyA As Integer = Val(PromotionRows(0).Item("PurQtyA") & "") ''購買量
                Dim PresentA As Integer = Val(PromotionRows(0).Item("PresentA") & "") ''贈送量

                Dim DiscountB As Double = Val(PromotionRows(0).Item("DiscountB") & "") * 0.01 ''折扣率(%)
                Dim ReduceB As Double = Val(PromotionRows(0).Item("ReduceB") & "") ''折扣金額
                Dim PurQtyB As Integer = Val(PromotionRows(0).Item("PurQtyB") & "") ''購買量
                Dim PresentB As Integer = Val(PromotionRows(0).Item("PresentB") & "") ''贈送量

                Select Case PromotionType

                    Case "一般折扣"

                        Dim IsDiscount As Boolean = False

                        If IsSame = True Then
                            If DiscountA > 0 Then
                                fGoodsSalePrice = fGoodsSalePrice * DiscountA
                                IsDiscount = True
                            Else
                                fGoodsSalePrice = fGoodsSalePrice - ReduceA
                            End If
                        Else
                            If DiscountB > 0 Then
                                fGoodsSalePrice = fGoodsSalePrice * DiscountB
                                IsDiscount = True
                            Else
                                fGoodsSalePrice = fGoodsSalePrice - ReduceB
                            End If
                        End If
                        GoodsSalePrice = fGoodsSalePrice
                        Dim currentRows() As DataRow

                        currentRows = mTableA.Select("BarCode='*" & BarCode & "*'  and SaleMoney>0")
                        If currentRows.Length > 0 Then
                            Dim fAmount As Integer = (currentRows(0).Item("mCount") & "") + fQuanty
                            currentRows(0).Item("mCount") = Format(fAmount, "#0.00")
                            currentRows(0).Item("SaleMoney") = Format(fAmount * fGoodsSalePrice, "#0.00")
                        Else
                            AddBarCodeToTable(BarCode, Rows, fGoodsSalePrice, fQuanty)
                            If IsDiscount = True Then
                                Me.TextBox2.Text += String.Format(Rows(0).Item("Code") & "享受{0}拆的促銷折扣。" & System.Environment.NewLine, IIf(IsSame = True, DiscountA * 10, DiscountB * 10))
                            Else
                                Me.TextBox2.Text += String.Format(Rows(0).Item("Code") & "享受{0}促銷現金折扣。" & System.Environment.NewLine, IIf(IsSame = True, ReduceA, ReduceB))
                            End If
                            PressF4Key()
                        End If
                        mTableA.AcceptChanges()


                    Case "買幾送幾"

                        ''添加主貨品
                        GoodsSalePrice = fGoodsSalePrice
                        Dim currentRows() As DataRow
                        currentRows = mTableA.Select("BarCode='*" & BarCode & "*'  and SaleMoney>0")
                        If currentRows.Length > 0 Then
                            Dim fAmount As Integer = (currentRows(0).Item("mCount") & "") + fQuanty
                            currentRows(0).Item("mCount") = Format(fAmount, "#0.00")
                            currentRows(0).Item("SaleMoney") = Format(fAmount * fGoodsSalePrice, "#0.00")
                        Else
                            AddBarCodeToTable(BarCode, Rows, fGoodsSalePrice, fQuanty)
                        End If

                        Dim purcherQuanty As Integer = Val(mTableA.Compute("sum(mCount)", "BarCode='*" & BarCode & "*'  and SaleMoney>0") & "")
                        Dim needQuanty As Integer = 0
                        Dim sendQuanty As Integer = 0

                        If IsSame = True Then
                            needQuanty = PurQtyA
                            sendQuanty = PresentA
                        Else
                            needQuanty = PurQtyB
                            sendQuanty = PresentB
                        End If
                        Dim Quanty As Integer = 0
                        Quanty = purcherQuanty \ needQuanty
                        If needQuanty > 0 AndAlso Quanty > 0 AndAlso sendQuanty > 0 Then

                            Dim fAmount As Integer = Quanty - (Val(mTableA.Compute("sum(mCount)", "BarCode='*" & BarCode & "*'  and SaleMoney=0") & "") \ sendQuanty)

                            'For i As Integer = 0 To Quanty

                            ''添加贈品
                            If Quanty > 0 AndAlso fAmount > 0 Then
                                Dim PRows() As DataRow = mTableA.Select("BarCode='*" & BarCode & "*'  and SaleMoney=0")

                                If PRows.Length > 0 Then
                                    PRows(0).Item("mCount") = Format(Quanty * sendQuanty, "#0.00")
                                    PRows(0).Item("SaleMoney") = Format(0, "#0.00")
                                Else
                                    AddBarCodeToTable(BarCode, Rows, 0, Quanty * sendQuanty, True)
                                    Dim key As String = "買幾送幾:" & Rows(0).Item("Code") & ""
                                    Me.TextBox2.Text += String.Format("{0} 購買滿{1}個,贈送{2}個。" & System.Environment.NewLine, key, needQuanty, sendQuanty)
                                    PressF4Key()
                                End If
                            End If
                        End If
                        mTableA.AcceptChanges()


                    Case "買A送B"

                        ''添加主貨品
                        GoodsSalePrice = fGoodsSalePrice
                        Dim currentRows() As DataRow
                        currentRows = mTableA.Select("BarCode='*" & BarCode & "*'  and SaleMoney>0")
                        If currentRows.Length > 0 Then
                            Dim fAmount As Integer = (currentRows(0).Item("mCount") & "") + fQuanty
                            currentRows(0).Item("mCount") = Format(fAmount, "#0.00")
                            currentRows(0).Item("SaleMoney") = Format(fAmount * fGoodsSalePrice, "#0.00")
                        Else
                            AddBarCodeToTable(BarCode, Rows, fGoodsSalePrice, fQuanty)
                        End If

                        ''添加贈品
                        Dim purcherQuanty As Integer = Val(mTableA.Compute("sum(mCount)", "BarCode='*" & BarCode & "*'  and SaleMoney>0") & "")
                        Dim needQuanty As Integer = 0
                        Dim sendQuanty As Integer = 0

                        If IsSame = True Then
                            needQuanty = PurQtyA
                            sendQuanty = PresentA
                        Else '如果不使用同一折扣
                            needQuanty = PurQtyB '主貨品
                            sendQuanty = PresentB
                        End If
                        Dim Quanty As Integer = 0

                        Dim subBarCode As String = ""
                        ''取得贈品編碼

                        Dim PRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & Num & "'  and  PromotionType<>'套裝優惠'   and GoodsType='贈送品'")
                        Me.TextBox2.Text += String.Format("購買{0}," & System.Environment.NewLine, Rows(0).Item("Code") & "")
                        For Each row As DataRow In PRows
                            If PRows.Length > 0 Then
                                subBarCode = row("BarCode").ToString
                            End If
                            If IsSame = True Then
                                needQuanty = PurQtyA
                                sendQuanty = PresentA
                            Else '如果不使用同一折扣
                                needQuanty = PurQtyB '主貨品
                                sendQuanty = row("PresentB").ToString
                            End If
                            Quanty = purcherQuanty \ needQuanty
                            If needQuanty > 0 AndAlso Quanty > 0 AndAlso sendQuanty > 0 AndAlso subBarCode.Length > 0 Then
                                Dim fAmount As Integer = Quanty - (Val(mTableA.Compute("sum(mCount)", "BarCode='" & subBarCode & "'  and SaleMoney=0") & "") \ sendQuanty)
                                If Quanty > 0 AndAlso fAmount > 0 Then
                                    Dim MRows() As DataRow = mTableA.Select("BarCode='" & subBarCode & "'  and SaleMoney=0")
                                    If MRows.Length > 0 Then
                                        MRows(0).Item("mCount") = Format(Quanty * sendQuanty, "#0.00")
                                        MRows(0).Item("SaleMoney") = Format(0, "#0.00")
                                    Else
                                        Dim QRows() As DataRow = mDataset.Tables(0).Select("BarCode='" & subBarCode & "'")
                                        If QRows.Length > 0 Then
                                            AddBarCodeToTable(subBarCode.Replace("*", ""), QRows, 0, Quanty * sendQuanty, True)
                                            Me.TextBox2.Text += String.Format("贈送{0}" & System.Environment.NewLine, QRows(0).Item("Code") & "")
                                            PressF4Key()
                                        End If
                                    End If
                                End If
                            End If
                        Next



                    Case Else
                        ''未知的方案
                        GoTo A

                End Select

            Else

A:
                ''三)是否是會員

                Dim fMemberRate As Double = 1.0 ''會員折扣率

                If Me.TextBox3.Text.Trim <> "" Then
                    Dim RowArray() As DataRow = DSMember.Tables(0).Select("Code='" & Me.TextBox3.Text & "'")
                    If RowArray.Length > 0 Then
                        fMemberRate = Val(RowArray(0).Item("Rat") & "")
                    End If
                End If
                fGoodsSalePrice = fGoodsSalePrice * fMemberRate
                GoodsSalePrice = fGoodsSalePrice
                Dim currentRows() As DataRow
                currentRows = mTableA.Select("BarCode='*" & BarCode & "*'  and SaleMoney>0")
                If currentRows.Length > 0 Then
                    Dim fAmount As Integer = (currentRows(0).Item("mCount") & "") + fQuanty
                    currentRows(0).Item("mCount") = Format(fAmount, "#0.00")
                    currentRows(0).Item("SaleMoney") = Format(fAmount * fGoodsSalePrice, "#0.00")
                Else
                    AddBarCodeToTable(BarCode, Rows, fGoodsSalePrice, fQuanty)
                End If

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message + ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            mTableA.AcceptChanges()
            Me.DataGridView1.DataSource = mTableA
            Me.txtSumMoney.Text = Val(mTableA.Compute("sum(SaleMoney)", "") & "")
            Me.DataGridView1.Focus()
            If DataGridView1.RowCount > 0 Then
                Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Cells("mCount")
            End If
            Me.UserDataGridView1.CurrentCell = Me.UserDataGridView1.Rows(0).Cells(2)


            IsOutPut = True
            Me.txtCount.Text = "1.00"
            Me.txtBarCode.Focus()
            Me.txtBarCode.Text = ""

        End Try
    End Sub


    Private Function GetCurrentSalefPrice(ByVal BarCode As String) As Double
        Dim SalePrice As Double = 0.0
        Dim sql As String = " select top 1 Isnull(SalePrice,0)  from Goods_SalePrice where BarCode='*" & BarCode & "*' and Convert(varchar(10),ValidDate,21)<=Convert(varchar(10),getdate(),21) order by ValidDate desc "

        Dim oo As Object
        oo = Exec(sql, E_ExecType.ExecuteScalar)

        SalePrice = Val(oo & "")

        Return SalePrice
    End Function

    ''' <summary>
    ''' 添加主貨品
    ''' </summary>
    ''' <param name="BarCode"></param>
    ''' <param name="Rows"></param>
    ''' <param name="fGoodsSalePrice"></param>
    ''' <param name="mCount"></param>
    ''' <remarks></remarks>
    Private Sub AddBarCodeToTable(ByVal BarCode As String, ByVal Rows() As DataRow, ByVal fGoodsSalePrice As Double, ByVal mCount As Integer, Optional ByVal isSub As Boolean = False)
        Dim NewRow As DataRow = mTableA.NewRow
        NewRow("GoodsCode") = mTableA.Rows.Count + 1
        NewRow("Code") = Rows(0).Item("Code") & ""
        NewRow("CnName") = Rows(0).Item("CnName") & IIf(isSub = True, "(贈品)", "")
        NewRow("mCount") = Format(mCount, "#0.00")
        NewRow("SaleMoney") = Format(fGoodsSalePrice * mCount, "#0.00")
        NewRow("BarCode") = "*" & BarCode & "*"
        mTableA.Rows.Add(NewRow)
        mTableA.AcceptChanges()
    End Sub

    Private Sub SuitPromotion(ByVal BarCode As String)
        Dim SRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & BarCode & "'  and PromotionType='套裝優惠' ")
        If SRows.Length > 0 Then

            Dim IsSame As Boolean = CType(SRows(0).Item("IsSame"), Boolean)

            For Each row As DataRow In SRows
                Dim subBarCode As String = (row("BarCode") & "").ToString.Replace("*", "")
                Dim DiscountA As Double = Val(row("DiscountA") & "") * 0.01 ''折扣率(%)
                Dim ReduceA As Double = Val(row("ReduceA") & "") ''折扣金額
                Dim PurQtyA As Integer = Val(row("PurQtyA") & "") ''購買量
                Dim PresentA As Integer = Val(row("PresentA") & "") ''贈送量

                Dim DiscountB As Double = Val(row("DiscountB") & "") * 0.01 ''折扣率(%)
                Dim ReduceB As Double = Val(row("ReduceB") & "") ''折扣金額
                Dim PurQtyB As Integer = Val(row("PurQtyB") & "") ''購買量
                Dim PresentB As Integer = Val(row("PresentB") & "") ''贈送量 


                Dim Rows() As DataRow = mDataset.Tables(0).Select("BarCode='*" & subBarCode & "*'")

                If Rows.Length > 0 Then

                    ''一)查找貨品當前銷售價格
                    Dim fGoodsSalePrice As Double = Val(Rows(0).Item("SalePrice"))
                    Dim currentSalePrice As Double = GetCurrentSalefPrice(BarCode)
                    If currentSalePrice > 0 Then
                        fGoodsSalePrice = currentSalePrice
                    End If

                    If IsSame = True Then
                        If DiscountA > 0 Then
                            fGoodsSalePrice = fGoodsSalePrice * DiscountA
                        Else
                            fGoodsSalePrice = fGoodsSalePrice - ReduceA
                        End If
                    Else
                        If DiscountB > 0 Then
                            fGoodsSalePrice = fGoodsSalePrice * DiscountB
                        Else
                            fGoodsSalePrice = fGoodsSalePrice - ReduceB
                        End If
                    End If

                    Dim PRows() As DataRow = mTableA.Select("BarCode='*" & subBarCode & "*'   and SaleMoney>0")
                    If PRows.Length > 0 Then
                        PRows(0).Item("mCount") = Format(Val(PRows(0).Item("mCount") & "") + 1, "#0.00")
                        PRows(0).Item("SaleMoney") = Format(Val(PRows(0).Item("SaleMoney") & "") + fGoodsSalePrice, "#0.00")
                    Else
                        AddBarCodeToTable(subBarCode, Rows, fGoodsSalePrice, 1)
                        Me.TextBox2.Text += "套裝優惠" + System.Environment.NewLine
                        PressF4Key()
                    End If
                End If
            Next
        End If
    End Sub

#End Region


#Region "Windows Events"

    ''' <summary>
    ''' 窗全加載事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Frm_SALPOS_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Dim B As Boolean = False
            ControlGrpBoxView(B)
            DataBindGridA()
            BindPayMoneyTypeGrid()
            SetEnable()

            Dim T As System.Threading.Thread = New System.Threading.Thread(New ThreadStart(AddressOf LoadGoods))
            T.Start()
            ''LoadGoods()
            LoadMemberPerson()
            LoadSalesPromotionSet()
            LoadSaleMan()
            LoadGifSet()
            PrintSet()
            Me.ComboBox1.SelectedIndex = -1
            Me.ComboBox1.Text = ""
            Me.lbTotalMoney.Text = "總金額：0.00" & "元"
            Me.txtMemberCode.Focus()
        Catch ex As Exception
            MsgBox("Frm_POS_Load:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub ControlGrpBoxView(ByVal B As Boolean)
        Try
            Me.GrpBoxAccount.Visible = B
            Me.GrpBoxUp.Visible = B
            Me.GrpBoxRemarks.Visible = B
            Me.GrpBoxPayMethod.Visible = B
            Me.GrpBoxMember.Visible = B
            Me.GrpBoxAccountView.Visible = B
        Catch ex As Exception

        End Try
    End Sub

    ''' <summary>
    ''' 保存數據
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function AddToDataBase() As Boolean
        Dim SqlConn As New SqlClient.SqlConnection(ConnectionString)
        SqlConn.Open()
        Dim Sqlcmd As SqlClient.SqlCommand = SqlConn.CreateCommand
        Dim SqlTran As SqlClient.SqlTransaction = Nothing
        Dim SqlBulk As SqlClient.SqlBulkCopy
        Try
            Dim sql As String = ""
            Dim GetSysDate As String = ReturnServerDate()
            Dim WriteOrdeNo As String = Me.TextBox1.Text.Replace("'", "")
            Dim CustomerNo As String = ""
            Dim SaleMan As String = Me.ComboBox1.SelectedValue & ""
            Dim CounterNo As String = ""
            Dim MoneyType As String = ""
            Dim PayMoney As Double = 0
            Dim PayCard As Double = 0
            Dim PayGift As Double = 0
            Dim PaySum As Double = 0
            Dim PayMoneyType As String = ""
            Dim PayMoneyCardNo As String = ""
            Dim PaySumMoney As Double = 0
            Me.LabAutoNo.Text = ""
            If Me.txtMemberCode.Text.Length <= 0 Then
                CustomerNo = ""
            Else
                CustomerNo = "*" & Me.txtMemberCode.Text & "*"
            End If
            PaySum = PayMoney + PayCard + PayGift
            Me.LabAutoNo.Text = AutoGetNo(PubCompanyCode, "SaleOrdeNo", "Sal_PosA")
            SqlTran = SqlConn.BeginTransaction
            Sqlcmd.Connection = SqlConn
            Sqlcmd.Transaction = SqlTran


            If mTableA.Rows.Count > 0 Then
                Dim GridDS As DataTable = CType(Me.UserDataGridView1.DataSource, DataTable)
                sql = "Insert Into Sal_PosA(SaleOrdeNo,WriteOrdeNo,CustomerNo,SaleMan,SaleDate,SumMoney,SumCount,OperateMan,OperateDate,ShopNo,CounterNo,LittleMoney,OrdeStatus,BuiltComputer) values('" & _
                                 Me.LabAutoNo.Text & "','" & WriteOrdeNo & "','" & CustomerNo & "','" & SaleMan & "','" & GetSysDate & "'," & Me.txtSumMoney.Text & "," & _
                                 mTableA.Compute("sum(mCount)", "") & ",'" & PubUserName & "','" & GetSysDate & "','" & PubCompanyCode & "','" & CounterNo & "','" & Me.txtLitMoney.Text & "',0,'" & My.Computer.Name & "');"
                If Me.TBGiftNum.Text <> "" Then
                    sql += "insert Into BSC_GiftUsed (Num,Satus,UseDate,UseDept,UseBy,UseComputer) values('" & Me.TBGiftNum.Text & "',1,'" & GetSysDate & "','" & PubCompanyCode & "','" & PubUserName & "','" & My.Computer.Name & "');"
                    sql += "update bsc_gift set Status = '1' where Num = '" & Me.TBGiftNum.Text & "';"

                End If
                With Sqlcmd
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                If Not GridDS Is Nothing Then
                    For i As Integer = 0 To GridDS.Rows.Count - 1
                        If Not GridDS.Rows(i).Item(2) Is DBNull.Value And GridDS.Rows(i).Item(2) <> "0.00" Then

                            PayMoneyType = GridDS.Rows(i).Item(0).ToString
                            PaySumMoney = GridDS.Rows(i).Item(2).ToString
                            PayMoneyCardNo = GridDS.Rows(i).Item(3).ToString
                            sql = " Insert Into Sal_PosC (SaleOrdeNo,PayMoneyType,PayMoneyCardNo,PayMoney,FCurrency,FRate,PayMoney_Loc)Values ('" & _
                                  Me.LabAutoNo.Text & "','" & PayMoneyType & "','" & PayMoneyCardNo & "'," & PaySumMoney & ",'HKD','1','" & PaySumMoney & "');"

                            With Sqlcmd
                                .CommandType = CommandType.Text
                                .CommandText = sql
                                .ExecuteNonQuery()
                            End With
                        End If
                    Next
                End If

                Dim mTable As DataTable = ReturnTable()
                SqlBulk = New SqlClient.SqlBulkCopy(SqlConn, SqlBulkCopyOptions.Default, SqlTran)
                SqlBulk.DestinationTableName = "dbo.Sal_PosB"
                SqlBulk.BulkCopyTimeout = 100
                SqlBulk.WriteToServer(mTable)
            Else
                Exit Function
            End If
            SqlTran.Commit()
            Return True
        Catch ex As Exception
            SqlTran.Rollback()
            MsgBox("AddToDataBase:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    ''' <summary>
    ''' 構造表並填充數據
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ReturnTable() As DataTable
        Try
            Dim nTable As New DataTable
            With nTable
                .Columns.Add("SaleOrdeNo")
                .Columns.Add("GoodsBarCode")
                .Columns.Add("GoodsCount")
                .Columns.Add("GoodsPrice")
            End With

            For Each Row As DataRow In mTableA.Rows
                Dim mRow As DataRow = nTable.NewRow
                mRow("SaleOrdeNo") = Me.LabAutoNo.Text.Trim
                mRow("GoodsBarCode") = Row("BarCode") & ""
                mRow("GoodsCount") = Row("mCount") & ""
                mRow("GoodsPrice") = Row("SaleMoney") & ""
                nTable.Rows.Add(mRow)
            Next
            nTable.AcceptChanges()
            Return nTable
        Catch ex As Exception
            MsgBox("ReturnTable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' 現金的回車事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtmMoney_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtmMoney.KeyDown
        Try
            Dim mMoney As Double = 0
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        Dim SumMoney As Double = 0
                        If CardMoney.Length <= 0 Then
                        Else
                            SumMoney = Double.Parse(CardMoney)
                        End If
                        'If Me.txtmMoney.Text.Length <= 0 Then
                        '    mMoney = 0.0
                        'Else
                        '    mMoney = Me.txtmMoney.Text
                        'End If
                        SumMoney = Val(txtSumMoney.Text)

                        Dim tb As DataTable = DirectCast(Me.UserDataGridView1.DataSource, DataTable)
                        Dim totalMoney As Double = Val(tb.Compute("sum(MoneyCount)", "") & "")

                        Dim Rows() As DataRow
                        Rows = tb.Select("ListA='CASH'")

                        If totalMoney = Val(Me.txtmMoney.Text) Then

                        Else
                            If Val(Rows(0).Item("MoneyCount") & "") = 0 Then
                                Rows(0).Item("MoneyCount") = Val(Me.txtmMoney.Text).ToString("#0.00")
                            Else
                                For Each r As DataRow In tb.Rows
                                    r("MoneyCount") = "0.00"
                                Next
                                Rows(0).Item("MoneyCount") = Val(Me.txtmMoney.Text).ToString("#0.00")
                            End If
                        End If

                        'If Rows.Length > 0 Then
                        '    Rows(0).Item("MoneyCount") = Val(Me.txtmMoney.Text).ToString("#0.00")
                        'End If

                        tb.AcceptChanges()

                        If sender.name = "txtmMoney" Then
                            Dim B As Boolean = False
                            ControlGrpBoxView(B)
                            Me.GrpBoxAccountView.Visible = True
                            Me.txtSumMoneyView.Text = Me.txtSumMoney.Text
                            Dim tb1 As DataTable = DirectCast(Me.UserDataGridView1.DataSource, DataTable)
                            Dim totalMoney1 As Double = Val(tb1.Compute("sum(MoneyCount)", "") & "")
                            Me.txtmMoneyView.Text = totalMoney1
                            Me.txtLitMoney.Text = totalMoney1 - Val(Me.txtSumMoney.Text)
                            'Me.GrpBoxAccountView.BringToFront()
                            'Me.GrpBoxAccountView.Left = (My.Computer.Screen.WorkingArea.Width + Me.GrpBoxAccount.Width) / 2
                            'Me.GrpBoxAccountView.Top = (My.Computer.Screen.WorkingArea.Width + Me.GrpBoxAccount.Height) / 2
                            Me.BtnConfimSale.Focus()
                        End If
                End Select
            End If
        Catch ex As Exception
            MsgBox("txtmMoney_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 設置現金的輸入類型
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtmMoney_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtmMoney.KeyPress
        Try
            If Char.IsControl(e.KeyChar) = True Then Exit Sub

            If Not IsNumeric(e.KeyChar) Then
                e.Handled = True
            Else
                e.Handled = False
            End If
        Catch ex As Exception
            MsgBox("txtmMoney_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 會員條碼回車
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtMemberCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMemberCode.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Dim Rows() As DataRow = DSMember.Tables(0).Select("Code='" & Me.txtMemberCode.Text & "'")
                If Rows.Length > 0 Then
                    For Each mRow As DataRow In Rows
                        Me.TextBox3.Text = mRow("Code").ToString.Replace("*", "")
                        Me.TextBox4.Text = mRow("Cnname") & ""
                        Me.TextBox5.Text = mRow("ListA") & ""
                        Me.TextBox6.Text = mRow("Rat") & ""
                        Me.TextBox7.Text = mRow("FIntegral") & ""
                    Next
                End If
                'Dim sql As String = ""
                'sql += "select a.ID,a.Code,a.Cnname,a.LinkMan,a.CnAddress,a.FType,a.Phone1,a.FLevel,a.FIntegral,b.ListA,b.ListB as Rat from BSC_Customer a left join "
                'sql += "(select b.* from dbo.BSC_OtherDefineA a inner join BSC_OtherDefineB b on a.ID=b.ItemNo where a.ItemCode='MemberLevel') b "
                'sql += "on a.FLevel=b.ListCode where a.Status=1 and a.FType='Retail' and a.code='" & Me.txtMemberCode.Text.Replace("*", "") & "'"

                'Dim dt As DataTable = New DataTable()
                'dt = Exec(sql, E_ExecType.DataTable)
                'If dt.Rows.Count > 0 Then
                '    Me.TextBox3.Text = dt.Rows(0).Item("Code").ToString.Replace("*", "")
                '    Me.TextBox4.Text = dt.Rows(0).Item("Cnname") & ""
                '    Me.TextBox5.Text = dt.Rows(0).Item("ListA") & ""
                '    Me.TextBox6.Text = dt.Rows(0).Item("Rat") & ""
                '    Me.TextBox7.Text = dt.Rows(0).Item("FIntegral") & ""
                'End If
                Me.txtMemberCode.Text = ""
                Me.txtBarCode.Focus()
                If Me.GrpBoxMember.Visible = False Then
                    GrpBoxMember.BringToFront()
                    GrpBoxMember.Visible = True
                End If
            End If
        Catch ex As Exception
            MsgBox("txtMemberCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' txtCount_GotFocus
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtCount_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCount.GotFocus
        Try
            Me.txtCount.SelectAll()
        Catch ex As Exception
            MsgBox("txtCount_GotFocus:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' txtCount_KeyDown
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtCount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCount.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                If Me.txtCount.Text.Length > 0 Then
                    'If Me.DataGridView1.Rows.Count > 0 Then
                    '    Me.DataGridView1.CurrentRow.Cells("mCount").Value = Me.txtCount.Text
                    '    Me.DataGridView1.Update()
                    'End If
                    Me.txtBarCode.Focus()
                Else
                    Me.txtBarCode.Focus()
                End If
            ElseIf e.KeyCode = Keys.F5 Then
                UserDataGridView1.Focus()
                Me.UserDataGridView1.CurrentCell = Me.UserDataGridView1.Rows(0).Cells(2)
            End If
        Catch ex As Exception
            MsgBox("txtCount_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' txtCount_KeyPress
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtCount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCount.KeyPress
        Try
            If Char.IsControl(e.KeyChar) = True Then Exit Sub

            If Not IsNumeric(e.KeyChar) Then
                e.Handled = True
            Else
                e.Handled = False
            End If
        Catch ex As Exception
            MsgBox("txtCount_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    '''' <summary>
    '''' 掃描會員條碼
    '''' </summary>
    '''' <param name="sender"></param>
    '''' <param name="e"></param>
    '''' <remarks></remarks>
    'Private Sub txtMemberCode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMemberCode.TextChanged
    '    Try

    '        'Dim Rows() As DataRow = DSMember.Tables(0).Select("Code='*" & Me.txtMemberCode.Text & "*'")
    '        'If Rows.Length > 0 Then
    '        '    For Each mRow As DataRow In Rows
    '        '        Me.TextBox3.Text = mRow("Code").ToString.Replace("*", "")
    '        '        Me.TextBox4.Text = mRow("Cnname") & ""
    '        '        Me.TextBox5.Text = mRow("ListA") & ""
    '        '        Me.TextBox6.Text = mRow("FIntegral") & ""
    '        '        Me.TextBox7.Text = mRow("Rat") & ""
    '        '    Next
    '        'End If
    '    Catch ex As Exception
    '        MsgBox("txtMemberCode_TextChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
    '    End Try
    'End Sub

    ''' <summary>
    ''' 銷售人員獲得焦點
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
            SendKeys.Send("{F4}")
        End If
    End Sub

    ''' <summary>
    ''' 貨品條碼框的回車事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtBarCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBarCode.KeyDown

        If e.KeyCode = Keys.Enter Then

            '    Try
            '        Dim sql As String = ""
            '        Dim m_ds As New DataSet
            '        Dim Rows() As DataRow = mDataset.Tables(0).Select("BarCode='*" & Me.txtBarCode.Text & "*'")

            '        If Rows.Length <= 0 Then
            '            '' 是否是次條碼 , 查找主條碼
            '            Dim MasterBarCode As String = GetMasterBarCode(Me.txtBarCode.Text.Trim)
            '            Rows = mDataset.Tables(0).Select("BarCode='*" & MasterBarCode & "*'")
            '            If Rows.Length > 0 Then '' 查找成功，使用主條碼
            '                Me.txtBarCode.Text = MasterBarCode
            '            End If
            '        End If

            '        Dim fMemberRate As Double = 1
            '        Dim fGoodsRate As Double = 1
            '        Dim fGoodsSalePrice As Double = 1
            '        Dim fGoodsLastPrice As Double = 0
            '        Dim SumMoney As Double = Val(Me.txtSumMoney.Text)

            '        IsOutPut = False
            '        If Rows.Length > 0 Then
            '            For Each mRow As DataRow In Rows
            '                fGoodsSalePrice = Val(mRow("SalePrice") & "")
            '            Next
            '        End If

            '        If Me.TextBox3.Text.Length > 0 Then
            '            Dim RowArray() As DataRow = DSMember.Tables(0).Select("Code='" & Me.TextBox3.Text & "'")
            '            If RowArray.Length > 0 Then
            '                For Each nRow As DataRow In RowArray
            '                    fMemberRate = nRow("Rat") & ""
            '                Next
            '            End If
            '        End If

            '        Dim ShopNo As String = ""
            '        Dim PromotionType As String = ""
            '        Dim Num As String = ""
            '        Dim DisCountA As Double = 0
            '        Dim DisCountB As Double = 0
            '        Dim ReduceA As Double = 0
            '        Dim ReduceB As Double = 0
            '        Dim PromotionRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & Me.txtBarCode.Text & "'")

            '        If PromotionRows.Length > 0 Then
            '            Me.LabPromotionInfo.Text = ""
            '            Me.LabPromotionInfo.Visible = True
            '            Dim PromotionMoney As Double
            '            Dim PurQtyA As Double = 0
            '            Dim PurQtyB As Double = 0
            '            Dim PresentA As Double = 0
            '            Dim PresentB As Double = 0
            '            Dim IsSame As Boolean
            '            Dim PromotionNum As String = ""
            '            Dim GoodsType As String = ""
            '            For Each mRow As DataRow In PromotionRows
            '                PromotionType = mRow("PromotionType") & ""
            '                ShopNo = mRow("Dept") & ""
            '                IsSame = mRow("IsSame") & ""
            '                PurQtyA = mRow("PurQtyA") & ""
            '                PresentA = mRow("PresentA") & ""
            '                PromotionNum = mRow("Num") & ""
            '                DisCountA = Convert.ToDouble(mRow("DisCountA") & "") / 100
            '                ReduceA = Convert.ToDouble(mRow("ReduceA") & "")
            '                Exit For
            '            Next

            '            If ShopNo.IndexOf(PubCompanyCode) <> -1 Then
            '                Select Case PromotionType.ToLower
            '                    Case "買A送B".ToLower
            '                        Me.LabPromotionInfo.Text = PromotionType
            '                        Dim NumRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & PromotionNum & "'")
            '                        If NumRows.Length > 0 Then
            '                            For Each nRow As DataRow In NumRows
            '                                Dim NewRow As DataRow = mTableA.NewRow
            '                                GoodsType = nRow("GoodsType") & ""
            '                                If GoodsType = "主貨品" Then
            '                                    Dim BarCodeRows() As DataRow = mDataset.Tables(0).Select("Barcode='" & nRow("Barcode") & "'")
            '                                    If BarCodeRows.Length > 0 Then
            '                                        NewRow("CnName") = BarCodeRows(0).Item("CnName") & ""
            '                                        NewRow("SaleMoney") = Format(Convert.ToDouble(BarCodeRows(0).Item("SalePrice")), "#0.00")
            '                                        PromotionMoney += Convert.ToDouble(BarCodeRows(0).Item("SalePrice"))
            '                                        NewRow("GoodsCode") = mTableA.Rows.Count + 1
            '                                        NewRow("mCount") = Format(1, "#0.00")
            '                                        NewRow("BarCode") = nRow("Barcode") & ""
            '                                        mTableA.Rows.Add(NewRow)
            '                                    End If
            '                                End If
            '                            Next
            '                            Me.txtSumMoney.Text = Val(Me.txtSumMoney.Text) + PromotionMoney
            '                            mTableA.AcceptChanges()
            '                            Me.DataGridView1.DataSource = mTableA
            '                            Me.txtBarCode.Text = ""
            '                            Me.txtBarCode.Focus()
            '                        End If
            '                    Case "套裝優惠".ToLower
            '                        Me.LabPromotionInfo.Text = PromotionType
            '                        Dim NumRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & PromotionNum & "'")
            '                        If NumRows.Length > 0 Then
            '                            For Each nRow As DataRow In NumRows
            '                                Dim Newrow As DataRow = mTableA.NewRow
            '                                DisCountB = Convert.ToDouble(nRow("DisCountB")) / 100
            '                                ReduceB = Convert.ToDouble(nRow("ReduceB"))
            '                                Dim BarCodeRows() As DataRow = mDataset.Tables(0).Select("BarCode='" & nRow("BarCode") & "'")
            '                                If BarCodeRows.Length > 0 Then
            '                                    Newrow("CnName") = BarCodeRows(0).Item("CnName") & ""
            '                                    Newrow("SaleMoney") = Format(Convert.ToDouble(BarCodeRows(0).Item("SalePrice")), "#0.00")
            '                                    PromotionMoney += Convert.ToDouble(BarCodeRows(0).Item("SalePrice"))
            '                                    Newrow("GoodsCode") = mTableA.Rows.Count + 1
            '                                    Newrow("mCount") = Format(1, "#0.00")
            '                                    Newrow("BarCode") = nRow("Barcode") & ""
            '                                    mTableA.Rows.Add(Newrow)
            '                                End If
            '                            Next
            '                            If IsSame = True Then
            '                                If DisCountA = 0.0 Then
            '                                    Me.txtSumMoney.Text += PromotionMoney - ReduceA
            '                                Else
            '                                    Me.txtSumMoney.Text += PromotionMoney * DisCountA
            '                                End If
            '                            Else
            '                                If DisCountB = 0.0 Then
            '                                    Me.txtSumMoney.Text += PromotionMoney - ReduceB
            '                                Else
            '                                    Me.txtSumMoney.Text += PromotionMoney * DisCountB
            '                                End If
            '                            End If
            '                            mTableA.AcceptChanges()
            '                            Me.DataGridView1.DataSource = mTableA
            '                            Me.txtBarCode.Text = ""
            '                            Me.txtBarCode.Focus()
            '                        End If
            '                End Select
            '            End If
            '        End If

            '        If Rows.Length > 0 Then
            '            Me.LabPromotionInfo.Text = ""
            '            Me.LabPromotionInfo.Visible = False
            '            Dim PurQtyB As Double = 0
            '            Dim PresentB As Double = 0
            '            Dim PurQtyA As Double = 0
            '            Dim PresentA As Double = 0
            '            Dim IsSame As Boolean
            '            Dim GoodsName As String = ""
            '            Dim GoodsType As String = ""
            '            Dim BuyAndDeliverRows() As DataRow = DSSalesPromotion.Tables(0).Select("Barcode='*" & Me.txtBarCode.Text & "*'")
            '            If BuyAndDeliverRows.Length > 0 Then
            '                Dim Dept As String = ""
            '                For Each nRow As DataRow In BuyAndDeliverRows
            '                    Dept = nRow("Dept") & ""
            '                    Num = nRow("Num") & ""
            '                    PromotionType = nRow("PromotionType") & ""
            '                    IsSame = nRow("IsSame")
            '                    If Dept.IndexOf(PubCompanyCode) <> -1 Then
            '                        If PromotionType.ToString = "買幾送幾" Then
            '                            If IsSame = False Then
            '                                PurQtyB = nRow("PurQtyB")
            '                                PresentB = nRow("PresentB")
            '                            Else
            '                                PurQtyA = nRow("PurQtyA")
            '                                PresentA = nRow("PresentA")
            '                            End If
            '                        ElseIf PromotionType.ToString = "買A送B" Then
            '                            Dim NumRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & Num & "'")
            '                            If NumRows.Length > 0 Then
            '                                For Each NumRow As DataRow In NumRows
            '                                    GoodsType = NumRow("GoodsType") & ""
            '                                    IsSame = NumRow("IsSame")
            '                                    If GoodsType.ToString = "主貨品" Then
            '                                        If IsSame = False Then
            '                                            PurQtyB = NumRow("PurQtyB")
            '                                        Else
            '                                            PurQtyA = NumRow("PurQtyA")
            '                                        End If
            '                                    ElseIf GoodsType.ToString = "贈送品" Then
            '                                        Dim BarCodeRows() As DataRow = mDataset.Tables(0).Select("Barcode='" & NumRow("BarCode") & "'")
            '                                        If BarCodeRows.Length > 0 Then
            '                                            GoodsName = BarCodeRows(0).Item("CnName").ToString
            '                                        End If
            '                                        If IsSame = False Then
            '                                            PresentB = NumRow("PresentB")
            '                                        Else
            '                                            PresentA = NumRow("PresentA")
            '                                        End If
            '                                    End If
            '                                Next
            '                            End If
            '                        End If
            '                    End If
            '                Next
            '            End If

            '            Dim GoodsRows() As DataRow = mTableA.Select("BarCode='*" & Me.txtBarCode.Text & "*'")
            '            If GoodsRows.Length > 0 Then
            '                For Each nRow As DataRow In GoodsRows
            '                    nRow("mCount") += 1
            '                    nRow("SaleMoney") = Convert.ToDouble(nRow("mCount")) * fGoodsSalePrice * fMemberRate
            '                Next
            '            Else
            '                For Each mRow As DataRow In Rows
            '                    Dim NewRow As DataRow = mTableA.NewRow
            '                    GoodsSalePrice = fGoodsSalePrice * fMemberRate
            '                    NewRow("GoodsCode") = mTableA.Rows.Count + 1
            '                    NewRow("Code") = mRow("Code") & ""
            '                    NewRow("CnName") = mRow("CnName") & ""
            '                    NewRow("mCount") = Format(1, "#0.00")
            '                    NewRow("SaleMoney") = Format(fGoodsSalePrice * fMemberRate, "#0.00")
            '                    NewRow("BarCode") = "*" & Me.txtBarCode.Text.Trim & "*"
            '                    mTableA.Rows.Add(NewRow)
            '                Next
            '            End If
            '            mTableA.AcceptChanges()
            '            Me.DataGridView1.DataSource = mTableA
            '            Dim BuyRowsCount() As DataRow = mTableA.Select("BarCode='*" & Me.txtBarCode.Text & "*'")
            '            Dim BuyGoodsCount As Double
            '            Dim BuyGoodsName As String = ""
            '            If BuyRowsCount.Length > 0 Then
            '                BuyGoodsCount = BuyRowsCount(0).Item("mCount")
            '                BuyGoodsName = BuyRowsCount(0).Item("CnName")
            '            End If
            '            If PromotionType.ToString = "買幾送幾" Then
            '                If IsSame = False Then
            '                    If BuyGoodsCount >= PurQtyB Then
            '                        Me.TextBox2.Text = "購買" & BuyGoodsCount & "個此貨品可贈送一個本貨品!"
            '                    End If
            '                Else
            '                    If BuyGoodsCount >= PurQtyA Then
            '                        Me.TextBox2.Text = "購買" & BuyGoodsCount & "個此貨品可贈送一個本貨品!"
            '                    End If
            '                End If
            '            ElseIf PromotionType.ToString = "買A送B" Then
            '                If IsSame = False Then
            '                    Me.TextBox2.Text = "購買" & PurQtyB & "個此貨品可獲贈" & GoodsName & PresentB & "個！"
            '                Else
            '                    Me.TextBox2.Text = "購買" & PurQtyA & "個此貨品可獲贈" & GoodsName & PresentA & "個！"
            '                End If
            '            End If

            '            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Cells("mCount")
            '            Me.txtSumMoney.Text = Convert.ToDouble(mTableA.Compute("sum(SaleMoney)", ""))
            '            Me.txtCount.Text = "1.00"
            '            Me.txtBarCode.Text = ""
            '            Me.txtBarCode.Focus()
            '        End If
            '        IsOutPut = True
            '        UserDataGridView1.Focus()
            '        Me.UserDataGridView1.CurrentCell = Me.UserDataGridView1.Rows(0).Cells(2)

            '    Catch ex As Exception
            '        MsgBox("txtBarCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
            '    End Try

            BarCodeScanInput()

        End If
        'Try
        '    If e.KeyCode = Keys.Shift Then
        '        Exit Sub
        '    Else
        '        If e.KeyCode = Keys.Enter Then
        '            If sender.name = "txtBarCode" Then
        '                UserDataGridView1.Focus()
        '                Me.UserDataGridView1.CurrentCell = Me.UserDataGridView1.Rows(0).Cells(2)
        '            End If
        '        Else
        '            'Me.txtBarCode.Focus()
        '        End If
        '    End If

        '    'Me.txtBarCode.Focus()
        'Catch ex As Exception
        '    MsgBox("txtBarCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        'End Try

    End Sub

    ''' <summary>
    ''' 取消交易
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            Me.txtMemberCode.Text = ""
            Me.txtBarCode.Text = ""
            Me.txtLitMoney.Text = "0.00"
            Me.txtSumMoney.Text = "0.00"
            Me.txtCount.Text = "1.00"
            Me.txtmMoney.Text = ""
            Me.txtmMoneyView.Text = ""
            txtSumMoneyView.Text = ""
            Me.TextBox1.Text = ""
            Me.ComboBox1.Text = ""
            Me.TextBox2.Text = ""

            Me.TextBox3.Text = ""
            Me.TextBox4.Text = ""
            Me.TextBox5.Text = ""
            Me.TextBox6.Text = ""
            Me.TextBox7.Text = ""
            Me.TBGiftNum.Text = ""
            Me.lblGiftMoney.Text = ""
            Me.ComboBox1.SelectedIndex = -1
            Me.ComboBox1.Text = ""
            mTableA.Rows.Clear()
            Me.DataGridView1.DataSource = mTableA
            For i As Integer = 0 To MoneyTypeDS.Tables(0).Rows.Count - 1
                MoneyTypeDS.Tables(0).Rows(i).Item("MoneyCount") = "0.00"
                MoneyTypeDS.Tables(0).Rows(i).Item("CARNO") = Nothing
            Next
            MoneyTypeDS.AcceptChanges()
            Me.UserDataGridView1.DataSource = MoneyTypeDS.Tables(0)
            GrpBoxAccountView.Visible = False
            Me.txtBarCode.Focus()
        Catch ex As Exception
            MsgBox("BtnCancel_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetPromotionSet()
        Try
            Dim ShopNo As String = ""
            Dim PromotionType As String = ""
            Dim Num As String = ""
            Dim PromotionRows() As DataRow = DSSalesPromotion.Tables(0).Select("Barcode='*" & Me.txtBarCode.Text & "*'")
            If PromotionRows.Length > 0 Then
                For Each mRow As DataRow In PromotionRows
                    Num = mRow("Num") & ""
                    Dim NumRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & Num & "'")
                    ShopNo = mRow("Dept") & ""
                    PromotionType = mRow("PromotionType") & ""

                    If ShopNo.IndexOf(PubCompanyCode) <> -1 Then

                        If NumRows.Length > 0 Then

                        End If

                        Select Case PromotionType.ToLower
                            Case "一般折扣".ToLower
                                Dim DisCount As Double = Convert.ToDouble(mRow("DisCountA") & "")
                            Case "買幾送幾".ToLower

                            Case "買A送B".ToLower

                            Case "套裝優惠".ToLower

                        End Select
                    End If
                Next

            End If

        Catch ex As Exception
            MsgBox("GetPromotionSet" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


    ''' <summary>
    ''' 掃描貨品條碼
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtBarCode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBarCode.TextChanged
        'Try
        '    Dim sql As String = ""
        '    Dim m_ds As New DataSet
        '    Dim Rows() As DataRow = mDataset.Tables(0).Select("BarCode='*" & Me.txtBarCode.Text & "*'")
        '    Dim fMemberRate As Double = 1
        '    Dim fGoodsRate As Double = 1
        '    Dim fGoodsSalePrice As Double = 1
        '    Dim fGoodsLastPrice As Double = 0
        '    Dim SumMoney As Double = Val(Me.txtSumMoney.Text)

        '    IsOutPut = False
        '    If Rows.Length > 0 Then
        '        For Each mRow As DataRow In Rows
        '            fGoodsSalePrice = mRow("SalePrice") & ""
        '        Next
        '    End If

        '    If Me.txtMemberCode.Text.Length > 0 Then
        '        Dim RowArray() As DataRow = DSMember.Tables(0).Select("Code='*" & Me.txtMemberCode.Text & "*'")
        '        If RowArray.Length > 0 Then
        '            For Each nRow As DataRow In RowArray
        '                fMemberRate = nRow("ListB") & ""
        '            Next
        '        End If
        '    End If

        '    Dim ShopNo As String = ""
        '    Dim PromotionType As String = ""
        '    Dim Num As String = ""
        '    Dim DisCountA As Double = 0
        '    Dim DisCountB As Double = 0
        '    Dim ReduceA As Double = 0
        '    Dim ReduceB As Double = 0
        '    Dim PromotionRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & Me.txtBarCode.Text & "'")

        '    If PromotionRows.Length > 0 Then
        '        Me.LabPromotionInfo.Text = ""
        '        Me.LabPromotionInfo.Visible = True
        '        Dim PromotionMoney As Double
        '        Dim PurQtyA As Double = 0
        '        Dim PurQtyB As Double = 0
        '        Dim PresentA As Double = 0
        '        Dim PresentB As Double = 0
        '        Dim IsSame As Boolean
        '        Dim PromotionNum As String = ""
        '        Dim GoodsType As String = ""
        '        For Each mRow As DataRow In PromotionRows
        '            PromotionType = mRow("PromotionType") & ""
        '            ShopNo = mRow("Dept") & ""
        '            IsSame = mRow("IsSame") & ""
        '            PurQtyA = mRow("PurQtyA") & ""
        '            PresentA = mRow("PresentA") & ""
        '            PromotionNum = mRow("Num") & ""
        '            DisCountA = Convert.ToDouble(mRow("DisCountA") & "") / 100
        '            ReduceA = Convert.ToDouble(mRow("ReduceA") & "")
        '            Exit For
        '        Next

        '        If ShopNo.IndexOf(PubCompanyCode) <> -1 Then
        '            Select Case PromotionType.ToLower
        '                Case "買A送B".ToLower
        '                    Me.LabPromotionInfo.Text = PromotionType
        '                    Dim NumRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & PromotionNum & "'")
        '                    If NumRows.Length > 0 Then
        '                        For Each nRow As DataRow In NumRows
        '                            Dim NewRow As DataRow = mTableA.NewRow
        '                            GoodsType = nRow("GoodsType") & ""
        '                            If GoodsType = "主貨品" Then
        '                                Dim BarCodeRows() As DataRow = mDataset.Tables(0).Select("Barcode='" & nRow("Barcode") & "'")
        '                                If BarCodeRows.Length > 0 Then
        '                                    NewRow("CnName") = BarCodeRows(0).Item("CnName") & ""
        '                                    NewRow("SaleMoney") = Format(Convert.ToDouble(BarCodeRows(0).Item("SalePrice")), "#0.00")
        '                                    PromotionMoney += Convert.ToDouble(BarCodeRows(0).Item("SalePrice"))
        '                                    NewRow("GoodsCode") = mTableA.Rows.Count + 1
        '                                    NewRow("mCount") = Format(1, "#0.00")
        '                                    NewRow("BarCode") = nRow("Barcode") & ""
        '                                    mTableA.Rows.Add(NewRow)
        '                                End If
        '                            End If
        '                        Next
        '                        Me.txtSumMoney.Text += PromotionMoney
        '                        mTableA.AcceptChanges()
        '                        Me.DataGridView1.DataSource = mTableA
        '                        Me.txtBarCode.Text = ""
        '                        Me.txtBarCode.Focus()
        '                    End If
        '                Case "套裝優惠".ToLower
        '                    Me.LabPromotionInfo.Text = PromotionType
        '                    Dim NumRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & PromotionNum & "'")
        '                    If NumRows.Length > 0 Then
        '                        For Each nRow As DataRow In NumRows
        '                            Dim Newrow As DataRow = mTableA.NewRow
        '                            DisCountB = Convert.ToDouble(nRow("DisCountB")) / 100
        '                            ReduceB = Convert.ToDouble(nRow("ReduceB"))
        '                            Dim BarCodeRows() As DataRow = mDataset.Tables(0).Select("BarCode='" & nRow("BarCode") & "'")
        '                            If BarCodeRows.Length > 0 Then
        '                                Newrow("CnName") = BarCodeRows(0).Item("CnName") & ""
        '                                Newrow("SaleMoney") = Format(Convert.ToDouble(BarCodeRows(0).Item("SalePrice")), "#0.00")
        '                                PromotionMoney += Convert.ToDouble(BarCodeRows(0).Item("SalePrice"))
        '                                Newrow("GoodsCode") = mTableA.Rows.Count + 1
        '                                Newrow("mCount") = Format(1, "#0.00")
        '                                Newrow("BarCode") = nRow("Barcode") & ""
        '                                mTableA.Rows.Add(Newrow)
        '                            End If
        '                        Next
        '                        If IsSame = True Then
        '                            If DisCountA = 0.0 Then
        '                                Me.txtSumMoney.Text += PromotionMoney - ReduceA
        '                            Else
        '                                Me.txtSumMoney.Text += PromotionMoney * DisCountA
        '                            End If
        '                        Else
        '                            If DisCountB = 0.0 Then
        '                                Me.txtSumMoney.Text += PromotionMoney - ReduceB
        '                            Else
        '                                Me.txtSumMoney.Text += PromotionMoney * DisCountB
        '                            End If
        '                        End If
        '                        mTableA.AcceptChanges()
        '                        Me.DataGridView1.DataSource = mTableA
        '                        Me.txtBarCode.Text = ""
        '                        Me.txtBarCode.Focus()
        '                    End If
        '            End Select
        '        End If
        '    End If

        '    If Rows.Length > 0 Then
        '        Me.LabPromotionInfo.Text = ""
        '        Me.LabPromotionInfo.Visible = False
        '        Dim PurQtyB As Double = 0
        '        Dim PresentB As Double = 0
        '        Dim PurQtyA As Double = 0
        '        Dim PresentA As Double = 0
        '        Dim IsSame As Boolean
        '        Dim GoodsName As String = ""
        '        Dim GoodsType As String = ""
        '        Dim BuyAndDeliverRows() As DataRow = DSSalesPromotion.Tables(0).Select("Barcode='*" & Me.txtBarCode.Text & "*'")
        '        If BuyAndDeliverRows.Length > 0 Then
        '            Dim Dept As String = ""
        '            For Each nRow As DataRow In BuyAndDeliverRows
        '                Dept = nRow("Dept") & ""
        '                Num = nRow("Num") & ""
        '                PromotionType = nRow("PromotionType") & ""
        '                IsSame = nRow("IsSame")
        '                If Dept.IndexOf(PubCompanyCode) <> -1 Then
        '                    If PromotionType.ToString = "買幾送幾" Then
        '                        If IsSame = False Then
        '                            PurQtyB = nRow("PurQtyB")
        '                            PresentB = nRow("PresentB")
        '                        Else
        '                            PurQtyA = nRow("PurQtyA")
        '                            PresentA = nRow("PresentA")
        '                        End If
        '                    ElseIf PromotionType.ToString = "買A送B" Then
        '                        Dim NumRows() As DataRow = DSSalesPromotion.Tables(0).Select("Num='" & Num & "'")
        '                        If NumRows.Length > 0 Then
        '                            For Each NumRow As DataRow In NumRows
        '                                GoodsType = NumRow("GoodsType") & ""
        '                                IsSame = NumRow("IsSame")
        '                                If GoodsType.ToString = "主貨品" Then
        '                                    If IsSame = False Then
        '                                        PurQtyB = NumRow("PurQtyB")
        '                                    Else
        '                                        PurQtyA = NumRow("PurQtyA")
        '                                    End If
        '                                ElseIf GoodsType.ToString = "贈送品" Then
        '                                    Dim BarCodeRows() As DataRow = mDataset.Tables(0).Select("Barcode='" & NumRow("BarCode") & "'")
        '                                    If BarCodeRows.Length > 0 Then
        '                                        GoodsName = BarCodeRows(0).Item("CnName").ToString
        '                                    End If
        '                                    If IsSame = False Then
        '                                        PresentB = NumRow("PresentB")
        '                                    Else
        '                                        PresentA = NumRow("PresentA")
        '                                    End If
        '                                End If
        '                            Next
        '                        End If
        '                    End If
        '                End If
        '            Next
        '        End If

        '        Dim GoodsRows() As DataRow = mTableA.Select("BarCode='*" & Me.txtBarCode.Text & "*'")
        '        If GoodsRows.Length > 0 Then
        '            For Each nRow As DataRow In GoodsRows
        '                nRow("mCount") += 1
        '                nRow("SaleMoney") = Convert.ToDouble(nRow("mCount")) * fGoodsSalePrice * fMemberRate
        '            Next
        '        Else
        '            For Each mRow As DataRow In Rows
        '                Dim NewRow As DataRow = mTableA.NewRow
        '                GoodsSalePrice = fGoodsSalePrice * fMemberRate
        '                NewRow("GoodsCode") = mTableA.Rows.Count + 1
        '                NewRow("Code") = mRow("Code") & ""
        '                NewRow("CnName") = mRow("CnName") & ""
        '                NewRow("mCount") = Format(1, "#0.00")
        '                NewRow("SaleMoney") = Format(fGoodsSalePrice * fMemberRate, "#0.00")
        '                NewRow("BarCode") = "*" & Me.txtBarCode.Text.Trim & "*"
        '                mTableA.Rows.Add(NewRow)
        '            Next
        '        End If
        '        mTableA.AcceptChanges()
        '        Me.DataGridView1.DataSource = mTableA
        '        Dim BuyRowsCount() As DataRow = mTableA.Select("BarCode='*" & Me.txtBarCode.Text & "*'")
        '        Dim BuyGoodsCount As Double
        '        Dim BuyGoodsName As String = ""
        '        If BuyRowsCount.Length > 0 Then
        '            BuyGoodsCount = BuyRowsCount(0).Item("mCount")
        '            BuyGoodsName = BuyRowsCount(0).Item("CnName")
        '        End If
        '        If PromotionType.ToString = "買幾送幾" Then
        '            If IsSame = False Then
        '                If BuyGoodsCount >= PurQtyB Then
        '                    Me.TextBox2.Text = "購買" & BuyGoodsCount & "個此貨品可贈送一個本貨品!"
        '                End If
        '            Else
        '                If BuyGoodsCount >= PurQtyA Then
        '                    Me.TextBox2.Text = "購買" & BuyGoodsCount & "個此貨品可贈送一個本貨品!"
        '                End If
        '            End If
        '        ElseIf PromotionType.ToString = "買A送B" Then
        '            If IsSame = False Then
        '                Me.TextBox2.Text = "購買" & PurQtyB & "個此貨品可獲贈" & GoodsName & PresentB & "個！"
        '            Else
        '                Me.TextBox2.Text = "購買" & PurQtyA & "個此貨品可獲贈" & GoodsName & PresentA & "個！"
        '            End If
        '        End If

        '        Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Cells("mCount")
        '        Me.txtSumMoney.Text = Convert.ToDouble(mTableA.Compute("sum(SaleMoney)", ""))
        '        Me.txtCount.Text = "1.00"
        '        Me.txtBarCode.Text = ""
        '        Me.txtBarCode.Focus()
        '    End If
        '    IsOutPut = True
        'Catch ex As Exception
        '    MsgBox("txtBarCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        'End Try
    End Sub

    ''' <summary>
    ''' GifBar 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtGifBar_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If GifDataSet Is Nothing Then Exit Sub

            ''If Me.UserDataGridView1.Rows(0).Cells(2) in 
            'If MoneyTypeDS.Tables(0).Rows(i).Item("listA ") = GifDataSet.Tables(0).Select Then

            'End If
            'Dim sql As String
            'Sql = " select  a.num  from "
            'sql += GifDataSet.Tables
            'Sql += "a inner join "
            'Sql += "MoneyTypeDS.Tables"
            'Sql += " b on a.num = b.carno"
            'Dim MoneyType() As DataRow
            'MoneyType() = MoneyTypeDS.Tables(0).Select("CarNo")
            'Dim DifDatsSet() As DataRow = GifDataSet.Tables(0).Select("Num")


            'If MoneyTypeDS.Tables(0).Select("CarNo") = GifDataSet.Tables(0).Select("Num") Then

            'End If


            Dim Rows() As DataRow = GifDataSet.Tables(0).Select("Num='" & "'")
            If Rows.Length > 0 Then
                'Me.txtGifMoney.Text = Rows(0).Item("FaceAmount")
            End If

        Catch ex As Exception
            MsgBox("txtGifBar_TextChanged:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 打印事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim mPaperSize As Printing.PaperSize
            Dim abc As String = ""

            If Printing.PrinterSettings.InstalledPrinters.Count <= 0 Then
                MessageBox.Show("专用发票打印机不存在！请设置名称为[GFZSFP]的打印机!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Exit Sub
            End If
            If Me.PrintDocument1.PrinterSettings.PaperSizes.Count <= 0 Then
                mPaperSize = New Printing.PaperSize("", 300, 600)
                Me.PrintDocument1.DefaultPageSettings.PaperSize = mPaperSize
            Else
                mPaperSize = New Printing.PaperSize("MyAAA", 350, 600)
                Me.PrintDocument1.DefaultPageSettings.PaperSize = mPaperSize
            End If
            Me.PrintPreviewDialog1.Document = Me.PrintDocument1
            If PrintSet() = "1" Then
                Me.PrintPreviewDialog1.ShowDialog()
            End If

            Me.BtnCancel_Click(Nothing, Nothing)
            Me.txtMemberCode.Focus()
        Catch ex As Exception
            MsgBox("Button2_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 打印頁面設置
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Try
            Dim linesPerPage As Single = 0
            Dim mPen As Pen = New Pen(Color.Black)
            mPen.DashStyle = Drawing2D.DashStyle.Dash
            Dim yPos As Single = 0
            Dim count As Integer = 0
            Dim line As String = Nothing
            Dim DrawWidth As Integer
            Dim Font1 As Font = New Font("宋体", 8, FontStyle.Regular)
            Dim Font2 As Font = New Font("宋体", 18, FontStyle.Bold)
            Dim Font3 As Font = New Font("宋体", 12, FontStyle.Bold)
            Dim Font4 As Font = New Font("IntHrP24DlTt", 15, FontStyle.Regular)
            Me.PrintDocument1.DefaultPageSettings.Margins.Left = 10
            Me.PrintDocument1.DefaultPageSettings.Margins.Top = 5

            Dim PrintAreaHeight, PrintAreaWidth, PrintAreaLeft, PrintAreaTop As Int32
            With Me.PrintDocument1.DefaultPageSettings
                PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
                PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
                PrintAreaLeft = .Margins.Left
                PrintAreaTop = .Margins.Top
            End With
            If PrintDocument1.DefaultPageSettings.Landscape Then
                Dim intTemp As Int32
                intTemp = PrintAreaHeight
                PrintAreaHeight = PrintAreaWidth
                PrintAreaWidth = intTemp
            End If

            Dim rectPrintingArea As New RectangleF(PrintAreaLeft, PrintAreaTop, PrintAreaWidth, PrintAreaHeight)
            x = Me.PrintDocument1.DefaultPageSettings.Margins.Left
            y = Me.PrintDocument1.DefaultPageSettings.Margins.Top
            Dim mHeight As Integer
            Dim mCount As Integer = 0
            DrawWidth = PrintAreaWidth / 2
            e.Graphics.DrawString("銷售小票", Font2, Brushes.Black, x + 130, y)
            e.Graphics.DrawString(PubCompanyName, Font3, Brushes.Black, x + 50, y + 35)
            e.Graphics.DrawString("地址：" & CompanyAddress, Font1, Brushes.Black, x + 40 + (Len(PubCompanyName) * 12) + 50, y + 30)
            e.Graphics.DrawString("電話：" & CompanyPhone & "傳真：" & CompanyFax, Font1, Brushes.Black, x + 40 + (Len(PubCompanyName) * 12) + 50, y + 45)

            e.Graphics.DrawString("日  期：" & Format(Now.Date.ToString("yyyy/MM/dd"), "yyyy/MM/dd"), Font1, Brushes.Black, x, y + 75)
            ' e.Graphics.DrawString("時  間：" & Format(Now.ToShortTimeString, "H:mm:ss"), Font1, Brushes.Black, x + 110, y + 75)
            e.Graphics.DrawString("時  間：" & DateTime.Now.ToString("H:mm:ss"), Font1, Brushes.Black, x + 110, y + 75)
            e.Graphics.DrawString("會  員：" & Me.TextBox4.Text, Font1, Brushes.Black, x + 200, y + 75)
            e.Graphics.DrawString("手寫單：" & Me.TextBox1.Text, Font1, Brushes.Black, x, y + 90)
            e.Graphics.DrawString("銷售員：" & Me.ComboBox1.SelectedValue & "", Font1, Brushes.Black, x + 110, y + 90)
            e.Graphics.DrawString("銷售單：" & Me.LabAutoNo.Text, Font1, Brushes.Black, x + 200, y + 90)

            e.Graphics.DrawLine(mPen, x, y + 103, 330, y + 103)
            e.Graphics.DrawString("貨品名稱" & Space(25) & "數量" & Space(8) & "金額", Font1, Brushes.Black, x, y + 104)
            e.Graphics.DrawLine(mPen, x, y + 118, 330, y + 118)
            For i As Integer = 0 To mTableA.Rows.Count - 1
                If i = 0 Then
                    mHeight = 5
                Else
                    mHeight = i * 30
                End If
                Dim GoodsCode As String = mTableA.Rows(i).Item("Code") & ""
                Dim GoodsName As String = mTableA.Rows(i).Item("CnName") & ""
                Dim GoodsCount As String = mTableA.Rows(i).Item("mCount") & ""
                Dim GoodsPrice As String = mTableA.Rows(i).Item("SaleMoney") & ""
                e.Graphics.DrawString(GoodsCode & Space(30) & GoodsCount & Space(8) & GoodsPrice, Font1, Brushes.Black, x, y + 120 + mHeight)
                e.Graphics.DrawString(GoodsName, Font1, Brushes.Black, x, y + 120 + mHeight + 12)
            Next
            mCount = mTableA.Rows.Count * 30
            e.Graphics.DrawLine(mPen, x, y + 120 + mCount, 330, y + 120 + mCount)
            e.Graphics.DrawString("總    計：" & Space(25) & mTableA.Compute("sum(mCount)", "") & Space(8) & Me.txtSumMoney.Text, Font1, Brushes.Black, x, y + 120 + mCount + 2)

            ''現金
            Dim cash As Double = 0.0
            Dim charge As Double = 0.0

            If Me.UserDataGridView1.RowCount > 1 Then
                Dim tb As DataTable = DirectCast(Me.UserDataGridView1.DataSource, DataTable)
                cash = Val(Me.UserDataGridView1.Rows(3).Cells(2).Value & "")
                charge = Val(tb.Compute("sum(MoneyCount)", "") & "") - cash
            End If

            e.Graphics.DrawString("現金收款：" & Space(33) & cash, Font1, Brushes.Black, x, y + 120 + mCount + 15)
            e.Graphics.DrawString("其他收款：" & Space(33) & charge, Font1, Brushes.Black, x, y + 120 + mCount + 30)
            'If CardMoney.Length > 0 Then
            '    e.Graphics.DrawString("其他收款：" & Space(33) & charge, Font1, Brushes.Black, x, y + 120 + mCount + 30)
            'Else
            '    e.Graphics.DrawString("其他收款：" & Space(33) & "0.00", Font1, Brushes.Black, x, y + 120 + mCount + 30)
            'End If
            If Me.txtLitMoney.Text.Length <= 0 Then
                e.Graphics.DrawString("找   零：" & Space(33) & "0.00", Font1, Brushes.Black, x, y + 120 + mCount + 45)
            Else
                e.Graphics.DrawString("找   零：" & Space(33) & Me.txtLitMoney.Text, Font1, Brushes.Black, x, y + 120 + mCount + 45)
            End If
            e.Graphics.DrawString("*" & Me.LabAutoNo.Text & "*", Font4, Brushes.Black, x, y + 120 + mCount + 60)
        Catch ex As Exception
            MsgBox("PrintDocument1_PrintPage:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    '''判斷網格單元格的數據是否正確
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
        Me.DataGridView1.Rows(e.RowIndex).ErrorText = Nothing
    End Sub

    ''' <summary>
    ''' 判斷網格單元格的數據是否正確
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        Try
            If e.ColumnIndex = 3 Then
                Dim testInteger As Double
                If Not e.FormattedValue.ToString.Length = 0 Then
                    If Not Double.TryParse(e.FormattedValue.ToString, testInteger) Then
                        'Me.LabError.Visible = True
                        'Me.LabError.Text = "错误: 无效的输入"
                        'Me.DGV_Master.Rows(e.RowIndex).ErrorText = "错误: 无效的输入"
                        e.Cancel = True
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("DataGridView1_CellValidating:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 網格單元格的值改變事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        Try
            Dim mCount As Double
            If e.ColumnIndex = 3 Then
                If mTableA.Rows.Count > 0 Then
                    mCount = Double.Parse(Me.DataGridView1.CurrentRow.Cells("mCount").Value.ToString)
                    Me.DataGridView1.CurrentRow.Cells("SaleMoney").Value = (mCount * GoodsSalePrice).ToString
                    DataGridView1.Update()
                    mTableA.AcceptChanges()
                    Me.DataGridView1.DataSource = mTableA
                    Me.txtSumMoney.Text = Convert.ToDouble(mTableA.Compute("sum(SaleMoney)", ""))
                End If
            End If

        Catch ex As Exception
            MsgBox("DataGridView1_CellValueChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 網格的鍵盤事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        Try

            Select Case e.KeyCode
                Case Keys.Delete
                    If Me.DataGridView1.Rows.Count <= 0 OrElse Me.DataGridView1.CurrentRow Is Nothing Then
                        Exit Sub
                    Else
                        'Dim Frm_PosManage As New Frm_PosManage
                        'Frm_PosManage.ShowDialog()
                        'If PosManage = True Then
                        '    Dim mIndex As Integer = Me.DataGridView1.CurrentRow.Cells("GoodsCode").Value
                        '    mTableA.Rows.RemoveAt(mIndex - 1)
                        '    mTableA.AcceptChanges()
                        '    Me.DataGridView1.DataSource = mTableA
                        '    Me.txtSumMoney.Text = Convert.ToDouble(mTableA.Compute("sum(SaleMoney)", ""))
                        '    Me.txtBarCode.Focus()
                        'End If
                        Dim mIndex As Integer = Me.DataGridView1.CurrentRow.Cells("GoodsCode").Value
                        mTableA.Rows.RemoveAt(mIndex - 1)
                        mTableA.AcceptChanges()
                        Me.DataGridView1.DataSource = mTableA
                        Me.txtSumMoney.Text = Val(mTableA.Compute("sum(SaleMoney)", "") & "")
                        Me.txtBarCode.Focus()
                    End If
                Case Keys.Enter
                    Me.txtBarCode.Focus()
            End Select

        Catch ex As Exception
            MsgBox("DataGridView1_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Function ComputeSumMoney() As Boolean

        Try

            Dim SumMoney As Double = 0
            If MoneyTypeDS Is Nothing Then Exit Function

            'SumMoney = MoneyTypeDS.Tables(0).Compute("Sum(MoneyCount)", "")
            For i As Int16 = 0 To Me.UserDataGridView1.Rows.Count - 1
                SumMoney += Val(Me.UserDataGridView1.Rows(i).Cells(2).Value)
            Next

            Me.txtmMoneyView.Text = SumMoney

            If SumMoney < Convert.ToDouble(Me.txtSumMoney.Text) Then
                MsgBox("對不起,金額不足!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.GrpBoxPayMethod.Visible = True
                Me.GrpBoxPayMethod.BringToFront()
                UserDataGridView1.Focus()
                Me.UserDataGridView1.CurrentCell = Me.UserDataGridView1.Rows(0).Cells(2)
                Return False
            Else
                Me.txtLitMoney.Text = SumMoney - Val(Me.txtSumMoney.Text)
            End If
            Return True
        Catch ex As Exception
            MsgBox("ComputeSumMoney:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Function

    ''' <summary>
    ''' 確認交易事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnConfimSale_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConfimSale.Click
        Try
            If Me.DataGridView1.Rows.Count <= 0 Then
                MsgBox("對不起，沒有單可交易！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)

                Me.txtBarCode.Focus()
                Exit Sub
            End If
            If ComputeSumMoney() = True Then
                If AddToDataBase() = True Then
                    ''會員
                    If Me.TextBox3.Text.Length > 0 Then
                        UpdateMemberMark()
                    End If
                    Button2_Click(Nothing, Nothing)
                    If UpdateGifNum() = False Then Exit Sub
                    BtnCancel_Click(Nothing, Nothing)
                Else
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MsgBox("BtnConfimSale_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' TabControl鍵盤事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>  
    Private Sub TabControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
        If Me.TabControl1.SelectedIndex = 0 Then
            Try
                Select Case e.KeyCode
                    Case Keys.F1
                        If Me.GrpBoxMember.Visible = True Then
                            Me.GrpBoxMember.Visible = False
                            Me.txtBarCode.Focus()
                        Else
                            Dim B As Boolean = False
                            ControlGrpBoxView(B)
                            Me.GrpBoxMember.Visible = True
                            Me.GrpBoxMember.Focus()
                            Me.GrpBoxMember.BringToFront()
                        End If
                    Case Keys.F2
                        If Me.GrpBoxUp.Visible = True Then
                            Me.GrpBoxUp.Visible = False
                        Else
                            Dim B As Boolean = False
                            ControlGrpBoxView(B)
                            Me.GrpBoxUp.Visible = True
                            Me.GrpBoxUp.Focus()
                            Me.GrpBoxUp.BringToFront()
                        End If
                    Case Keys.F3
                        If Me.GrpBoxPayMethod.Visible = True Then
                            Me.GrpBoxPayMethod.Visible = False
                            Me.txtBarCode.Focus()
                        Else
                            Dim B As Boolean = False
                            ControlGrpBoxView(B)
                            Me.GrpBoxPayMethod.Visible = True
                            Me.GrpBoxPayMethod.BringToFront()
                            Me.GrpBoxPayMethod.Focus()
                            Me.UserDataGridView1.Focus()
                            If Me.UserDataGridView1.Rows.Count > 2 Then
                                Me.UserDataGridView1.CurrentCell = Me.UserDataGridView1.Rows(1).Cells("MoneyCount")
                            End If
                        End If
                    Case Keys.F4

                        PressF4Key()

                    Case Keys.F5
                        'If Me.GrpBoxAccount.Visible = False Then
                        '    Dim B As Boolean = False
                        '    ControlGrpBoxView(B)
                        '    Me.GrpBoxAccount.Visible = True
                        '    Me.GrpBoxAccount.BringToFront()
                        '    Me.GrpBoxAccount.Focus()
                        '    Dim dt As DataTable = DirectCast(Me.UserDataGridView1.DataSource, DataTable)
                        '    Dim fAmount As Double = Val(dt.Compute("sum(MoneyCount)", ""))
                        '    If fAmount > 0 And Me.DataGridView1.RowCount > 0 Then
                        '        Me.txtmMoney.Text = fAmount
                        '    End If
                        '    Me.txtmMoney.Focus()
                        'Else
                        '    Me.GrpBoxAccount.Visible = False
                        '    Me.txtBarCode.Focus()

                        'End If
                        PressF5Key()
                    Case Keys.Escape


                        If MessageBox.Show("是否要取消該單？", Msg.CompanyName, MessageBoxButtons.OKCancel) = DialogResult.OK Then
                            Dim B As Boolean = False
                            ControlGrpBoxView(B)
                            Me.BtnCancel_Click(Nothing, Nothing)
                        End If

                    Case Keys.F6

                        PutSaleOrdeUp()

                End Select



            Catch ex As Exception
                MsgBox("TabControl1_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
            End Try

        End If

        ''如果是掛單
        If Me.GrpBoxUp.Controls.Count > 0 Then
            Dim mTag As String = ""
            If Me.GrpBoxUp.Controls.Count > 0 Then
                For i As Integer = 0 To Me.GrpBoxUp.Controls.Count - 1
                    mTag += Me.GrpBoxUp.Controls(i).Tag + ","
                Next
            End If


            Select Case e.KeyCode
                Case Keys.D1
                    If mTag.IndexOf("1") = -1 Then
                        Exit Sub
                    Else
                        If Me.DataGridView1.Rows.Count > 0 Then
                            MsgBox(My.Resources.PleaseComplete, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.txtBarCode.Focus()
                            Exit Sub
                        Else
                            mTableA = F1Table.Copy
                            Me.DataGridView1.DataSource = mTableA
                            Me.txtSumMoney.Text = mTableA.Compute("sum(SaleMoney)", "").ToString
                            For Each mValue As Control In Me.GrpBoxUp.Controls
                                If mValue.Tag = "1" Then
                                    Me.GrpBoxUp.Controls.Remove(mValue)
                                End If
                            Next
                            F1Table.Rows.Clear()
                            Me.F1Table.AcceptChanges()
                            FillMemberInfo("1")
                            Me.txtBarCode.Focus()
                        End If
                    End If
                Case Keys.D2
                    If mTag.IndexOf("2") = -1 Then
                        Exit Sub
                    Else
                        If Me.DataGridView1.Rows.Count > 0 Then
                            MsgBox(My.Resources.PleaseComplete, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.txtBarCode.Focus()
                            Exit Sub
                        Else
                            mTableA = F2Table.Copy
                            Me.DataGridView1.DataSource = mTableA
                            Me.txtSumMoney.Text = mTableA.Compute("sum(SaleMoney)", "").ToString
                            For Each mValue As Control In Me.GrpBoxUp.Controls
                                If mValue.Tag = "2" Then
                                    Me.GrpBoxUp.Controls.Remove(mValue)
                                End If
                            Next
                            F2Table.Rows.Clear()
                            Me.F2Table.AcceptChanges()
                            FillMemberInfo("2")
                            Me.txtBarCode.Focus()
                        End If
                    End If
                Case Keys.D3
                    If mTag.IndexOf("3") = -1 Then
                        Exit Sub
                    Else
                        If Me.DataGridView1.Rows.Count > 0 Then
                            MsgBox(My.Resources.PleaseComplete, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.txtBarCode.Focus()
                            Exit Sub
                        Else
                            mTableA = F3Table.Copy
                            Me.DataGridView1.DataSource = mTableA
                            Me.txtSumMoney.Text = mTableA.Compute("sum(SaleMoney)", "").ToString
                            For Each mValue As Control In Me.GrpBoxUp.Controls
                                If mValue.Tag = "3" Then
                                    Me.GrpBoxUp.Controls.Remove(mValue)
                                End If
                            Next
                            F3Table.Rows.Clear()
                            Me.F3Table.AcceptChanges()
                            FillMemberInfo("3")
                            Me.txtBarCode.Focus()
                        End If
                    End If
                Case Keys.D4
                    If mTag.IndexOf("4") = -1 Then
                        Exit Sub
                    Else
                        If Me.DataGridView1.Rows.Count > 0 Then
                            MsgBox(My.Resources.PleaseComplete, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.txtBarCode.Focus()
                            Exit Sub
                        Else
                            mTableA = F4Table.Copy
                            Me.DataGridView1.DataSource = mTableA
                            Me.txtSumMoney.Text = mTableA.Compute("sum(SaleMoney)", "").ToString
                            For Each mValue As Control In Me.GrpBoxUp.Controls
                                If mValue.Tag = "4" Then
                                    Me.GrpBoxUp.Controls.Remove(mValue)
                                End If
                            Next
                            F4Table.Rows.Clear()
                            Me.F4Table.AcceptChanges()
                            FillMemberInfo("4")
                            Me.txtBarCode.Focus()
                        End If
                    End If
                    'Case Keys.F5
                    '    UserDataGridView1.Focus()
                    '    Me.UserDataGridView1.CurrentCell = Me.UserDataGridView1.Rows(0).Cells(2)
                    'Case Keys.F6
                    '    PutSaleOrdeUp()
                    'Case Keys.Escape
                    '    BtnCancel_Click(Nothing, Nothing)
            End Select
        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub UserDataGridView1_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Me.UserDataGridView1.Rows(e.RowIndex).ErrorText = ""
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub UserDataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs)
        Try
            If e.ColumnIndex = 2 Then
                Dim testInteger As Double
                If Not e.FormattedValue.ToString.Length = 0 Then
                    If Not Double.TryParse(e.FormattedValue.ToString, testInteger) Then
                        e.Cancel = True
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("UserDataGridView1_CellValidating:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


    ''' <summary>
    ''' Press F5 key
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub PressF5Key()
        If Me.GrpBoxAccount.Visible = False Then
            Dim B As Boolean = False
            ControlGrpBoxView(B)
            Me.GrpBoxAccount.Visible = True
            Me.GrpBoxAccount.BringToFront()
            Me.GrpBoxAccount.Focus()
            Dim dt As DataTable = DirectCast(Me.UserDataGridView1.DataSource, DataTable)
            Dim fAmount As Double = Val(dt.Compute("sum(MoneyCount)", ""))
            If fAmount > 0 And Me.DataGridView1.RowCount > 0 Then
                Me.txtmMoney.Text = fAmount
                Me.txtlevemoney.Text = fAmount - Val(Me.txtSumMoney.Text)
            End If
            Me.txtmMoney.Focus()
        Else
            Me.GrpBoxAccount.Visible = False
            Me.txtBarCode.Focus()
        End If
    End Sub

    Private Sub PressF4Key()
        If Me.GrpBoxRemarks.Visible = True Then
            Me.GrpBoxRemarks.Visible = False
        Else
            Dim B As Boolean = False
            ControlGrpBoxView(B)
            Me.GrpBoxRemarks.Visible = True
            Me.GrpBoxRemarks.BringToFront()
        End If
    End Sub

#End Region

#End Region


#Region "TabControlPage2"

    ''' <summary>
    ''' 綁定網格,設置網格的樣式
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindGridView4()
        Try
            mTableD = New DataTable
            With mTableD
                .Columns.Add("SaleOrdeNo", GetType(String))
                .Columns.Add("GoodsBarCode", GetType(String))
                .Columns.Add("GoodsCode", GetType(String))
                .Columns.Add("GoodsName", GetType(String))
                .Columns.Add("GoodsColor", GetType(String))
                .Columns.Add("GoodsSize", GetType(String))
                .Columns.Add("GoodsCount", GetType(Decimal))
                .Columns.Add("SaleMoney", GetType(Decimal))
                .Columns.Add("BuyDate", GetType(String))
                .Columns.Add("SalePrice", GetType(String))
            End With
            'Me.DGV_Master.DataSource = mTableD

            'With Me.DGV_Master
            '    For i As Integer = 0 To .Columns.Count - 1
            '        Dim mKey As String = .Columns(i).Name
            '        Dim CaptionStr As String = BindHash(2).Item(mKey)
            '        .Columns(i).HeaderText = CaptionStr
            '        If .Columns(i).Name = "GoodsCount" Then
            '            .Columns(i).ReadOnly = False
            '            .Columns(i).DefaultCellStyle.BackColor = Color.LightYellow
            '        Else
            '            .Columns(i).ReadOnly = True
            '            .Columns(i).DefaultCellStyle.BackColor = Color.LightGray
            '        End If
            '    Next
            '    .AllowUserToAddRows = False
            '    .AllowUserToDeleteRows = False
            '    .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            '    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
            '    .SelectionMode = DataGridViewSelectionMode.CellSelect
            '    .Columns("SalePrice").Visible = False
            'End With

        Catch ex As Exception
            MsgBox("BindGridView4:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 加載銷售單
    ''' </summary>
    ''' <param name="WhereStr"></param>
    ''' <remarks></remarks>
    Private Sub GetData(ByVal WhereStr As String)
        Try
            Dim sql As String = ""
            sql = "select distinct(replace(a.GoodsBarCode,'*','')) as GoodsBarCode,a.SaleOrdeNo,b.Code as GoodsCode,b.CnName as GoodsName,c.ColorCName as GoodsColor,d.SizeSName as GoodsSize,a.GoodsCount,a.GoodsPrice as SaleMoney,e.SaleDate as BuyDate,b.SalePrice from dbo.Sal_PosB a left join BSC_Goods b on a.GoodsBarCode=b.BarCode inner join BSC_ColorDefine c on b.Color=c.ColorCode inner join BSC_SizeDefine d on b.Spec=d.SizeCode left join Sal_PosA e on a.SaleOrdeNo=e.SaleOrdeNo where a.SaleOrdeNo='" & WhereStr & "'"
            mTableD = New DataTable
            mTableD = ReturnDataTable(sql)
            'Me.DGV_Master.DataSource = mTableD
        Catch ex As Exception
            MsgBox("GetData:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


    ''' <summary>
    ''' TabControl選擇後事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        Try
            If Me.TabControl1.SelectedIndex = 1 Then
                BindGridView4()
                'Me.txtSaleNo.Focus()
            Else
                Me.txtBarCode.Focus()
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selected:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' TabControl選擇時事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        Try

        Catch ex As Exception
            MsgBox("TabControl1_Selecting:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' txtSaleNo回車事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtSaleNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    'GetData(Me.txtSaleNo.Text.Trim)
            End Select
        Catch ex As Exception
            MsgBox("txtSaleNo_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' txtSaleNo的TextChanged事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtSaleNo_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            'GetData(Me.txtSaleNo.Text.Trim)
        Catch ex As Exception
            MsgBox("txtSaleNo_TextChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 判斷退貨處理網格單元格的數據
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DGV_Master_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        'Me.LabError.Text = ""
        'Me.LabError.Visible = False
        'Me.DGV_Master.Rows(e.RowIndex).ErrorText = Nothing
    End Sub

    ''' <summary>
    ''' 出錯處理事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DGV_Master_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs)

        Try
            If e.ColumnIndex = 6 Then
                Dim testInteger As Double
                If Not e.FormattedValue.ToString.Length = 0 Then
                    If Not Double.TryParse(e.FormattedValue.ToString, testInteger) Then
                        'Me.LabError.Visible = True
                        'Me.LabError.Text = "错误: 无效的输入"
                        'Me.DGV_Master.Rows(e.RowIndex).ErrorText = "错误: 无效的输入"
                        'e.Cancel = True
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("DGV_Master_CellValidating:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 單元格改變事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DGV_Master_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Try
            If e.ColumnIndex = 6 Then
                ''Dim SalePrice As Double
                '' Dim GoodsCount As Double
                If mTableD.Rows.Count > 0 Then
                    'If Me.DGV_Master.CurrentRow.Cells("SalePrice").Value Is System.DBNull.Value Then
                    'Else
                    '    SalePrice = Convert.ToDouble(Me.DGV_Master.CurrentRow.Cells("SalePrice").Value)
                    'End If
                    'If Me.DGV_Master.CurrentRow.Cells("GoodsCount").Value Is System.DBNull.Value Then
                    'Else
                    '    GoodsCount = Convert.ToDouble(Me.DGV_Master.CurrentRow.Cells("GoodsCount").Value)
                    'End If
                    'If Not IsNumeric(GoodsCount) Then
                    '    Exit Sub
                    'End If
                    'Me.DGV_Master.CurrentRow.Cells("SaleMoney").Value = Convert.ToDouble(GoodsCount) * Convert.ToDouble(SalePrice)
                End If
            End If
        Catch ex As Exception
            MsgBox("DGV_Master_CellValueChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 網格的鍵盤事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DGV_Master_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If Me.TabControl1.SelectedIndex = 1 Then
                Select Case e.KeyCode
                    Case Keys.Delete
                        If DeleteRowOfGrid() = False Then
                            Exit Sub
                        End If
                End Select
            End If
        Catch ex As Exception
            MsgBox("DGV_Master_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 刪除網格中的行
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DeleteRowOfGrid() As Boolean

        Dim CurrentCell As String = ""
        Try
            'CurrentCell = Me.DGV_Master.CurrentRow.Cells("GoodsBarCode").Value
            If CurrentCell.Length <= 0 Then
                Return False
            End If
            Dim Rows() As DataRow = mTableD.Select("GoodsBarCode='" & CurrentCell & "'")

            If Rows.Length > 0 Then
                If MsgBox(My.Resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                    For Each mRow As DataRow In Rows
                        mRow.Delete()
                    Next
                End If
            Else
                Return False
            End If
            mTableD.AcceptChanges()
            'Me.DGV_Master.DataSource = mTableD
            Return True
        Catch ex As Exception
            MsgBox("DeleteRowOfGrid:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Function

    ''' <summary>
    ''' 保存數據
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim SqlConn As New SqlClient.SqlConnection(ConnectionString)
        SqlConn.Open()
        Dim Sqlcmd As SqlClient.SqlCommand = SqlConn.CreateCommand
        Dim SqlTran As SqlClient.SqlTransaction = Nothing
        Dim SqlBulk As SqlClient.SqlBulkCopy
        Dim sql As String = ""
        Dim SumCount As Double
        Dim SumMoney As Double
        Try
            SqlTran = SqlConn.BeginTransaction
            Sqlcmd.Connection = SqlConn
            Sqlcmd.Transaction = SqlTran

            If mTableD.Rows.Count > 0 Then
                SumCount = mTableD.Compute("sum(GoodsCount)", "")
                SumMoney = mTableD.Compute("sum(SaleMoney)", "")
                sql = "Insert Into SAL_GoodsBACK(SaleOrdeNo,GoodsBarcode,GoodsCount,GoodsMoney,EditPerson,EditDate,EditCompute,shopNo)"
                'sql += "select SaleOrdeNo,GoodsBarCode,GoodsCount,GoodsPrice,EditPerson='" & PubUserName & "',EditDate='" & Format(Now.Date, "yyyy-MM-dd") & "',EditCompute='" & My.Computer.Name & "',ShopNo='" & PubCompanyCode & "' from Sal_PosB where SaleOrdeNo='" & Me.txtSaleNo.Text & "'"
                With Sqlcmd
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                'sql = "Delete Sal_PosB where SaleOrdeNo='" & Me.txtSaleNo.Text & "'"
                With Sqlcmd
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                'sql = "Update Sal_PosA Set SumCount=" & SumCount & ",SumMoney=" & SumMoney & ",OrdeStatus=1,UpdateDate=convert(varchar(10),getdate(),21),UpdateComputer='" & My.Computer.Name & "'  where SaleOrdeNo='" & Me.txtSaleNo.Text & "'"
                With Sqlcmd
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With
                Dim ColName As String = "SaleOrdeNo,GoodsBarCode,GoodsCount,GoodsPrice"
                Dim mDataTable As DataTable = InticntDataTable(ColName)
                SqlBulk = New SqlClient.SqlBulkCopy(SqlConn, SqlBulkCopyOptions.Default, SqlTran)
                SqlBulk.DestinationTableName = "dbo.Sal_PosB"
                SqlBulk.BulkCopyTimeout = 100
                SqlBulk.WriteToServer(mDataTable)
            Else
                Exit Sub
            End If
            SqlTran.Commit()
            MsgBox("操作成功!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            'Me.DGV_Master.Columns(6).ReadOnly = True
            'Me.DGV_Master.Columns(6).DefaultCellStyle.BackColor = Color.LightGray
        Catch ex As Exception
            SqlTran.Rollback()
            MsgBox("BtnSave_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 初始化表並填充數據
    ''' </summary>
    ''' <param name="ColName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function InticntDataTable(ByVal ColName As String) As DataTable
        Try
            Dim mTable As New DataTable
            Dim Str() As String = ColName.Split(",")
            With mTable
                For i As Integer = 0 To Str.Length - 1
                    .Columns.Add(Str(i))
                Next
            End With

            For Each Row As DataRow In mTableD.Rows
                Dim mRow As DataRow = mTable.NewRow
                'mRow("SaleOrdeNo") = Me.txtSaleNo.Text.Trim
                mRow("GoodsBarCode") = "*" & Row("GoodsBarCode") & "*"
                mRow("GoodsCount") = Row("GoodsCount") & ""
                mRow("GoodsPrice") = Row("SaleMoney") & ""
                mTable.Rows.Add(mRow)
            Next
            mTable.AcceptChanges()
            Return mTable
        Catch ex As Exception
            Return Nothing
            MsgBox("BtnSave_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub UserDataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UserDataGridView1.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Me.UserDataGridView1.EndEdit()
                Me.UserDataGridView1.Update()
                PressF5Key()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub txtSumMoney_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSumMoney.TextChanged
        Me.lbTotalMoney.Text = String.Format("總金額：{0}", Val(Me.txtSumMoney.Text).ToString("#0.00"))
    End Sub
#End Region

    ''' <summary>
    ''' 禮券回車事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TBGiftNum_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TBGiftNum.KeyDown
        Try
            Me.lblGiftMoney.Text = ""
            If e.KeyCode = Keys.Enter Then
                Dim Rows() As DataRow = GifDataSet.Tables(0).Select("Num='" & Me.TBGiftNum.Text & "'")
                If Rows.Length > 0 Then
                    For Each mRow As DataRow In Rows
                        Me.lblGiftMoney.Text = mRow("FaceAmount") & "元"
                    Next
                Else
                    MessageBox.Show("對不起，禮券號：" & Me.TBGiftNum.Text & "  已使用或已過期！", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Me.TBGiftNum.Text = ""
                End If
                UserDataGridView1.Focus()
            End If
        Catch ex As Exception
            MsgBox("TBGiftNum_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub GroupBox6_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox6.Enter

    End Sub

    Private Sub TBGiftNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBGiftNum.TextChanged
        'Try
        '    Me.lblGiftMoney.Text = ""

        '    Dim Rows() As DataRow = GifDataSet.Tables(0).Select("Num like '%" & Me.TBGiftNum.Text & "%'")
        '    If Rows.Length > 0 Then
        '        For Each mRow As DataRow In Rows
        '            Me.lblGiftMoney.Text = mRow("FaceAmount") & "元"
        '        Next
        '    Else
        '        MessageBox.Show("對不起，禮券號：" & Me.TBGiftNum.Text & "  已使用或已過期！", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        '        Me.TBGiftNum.Text = ""
        '    End If
        '    UserDataGridView1.Focus()

        'Catch ex As Exception
        '    MsgBox("TBGiftNum_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        'End Try
    End Sub

    Private Sub TBGiftNum_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBGiftNum.Leave
        Try
            Me.lblGiftMoney.Text = ""
            If Me.TBGiftNum.Text <> "" Then

                Dim Rows() As DataRow = GifDataSet.Tables(0).Select("Num='" & Me.TBGiftNum.Text & "'")
                If Rows.Length > 0 Then
                    For Each mRow As DataRow In Rows
                        Me.lblGiftMoney.Text = mRow("FaceAmount") & "元"
                    Next
                Else
                    MessageBox.Show("對不起，禮券號：" & Me.TBGiftNum.Text & "  已使用或已過期！", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Me.TBGiftNum.Text = ""
                End If

            End If

            UserDataGridView1.Focus()

        Catch ex As Exception
            MsgBox("TBGiftNum_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtMemberCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMemberCode.TextChanged

    End Sub

    Private Sub lblGiftMoney_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblGiftMoney.TextChanged
        If Me.lblGiftMoney.Text <> "" Then

            ''MessageBox.Show("禮券號：" & Me.TBGiftNum.Text & "面值:" & Me.lblGiftMoney.Text, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Dim Table As DataTable = DirectCast(Me.UserDataGridView1.DataSource, DataTable)

            Dim Rows() As DataRow

            Rows = Table.Select("ListA='GIFT(禮券)'")

            Rows(0).Item("MoneyCount") = Val(Me.lblGiftMoney.Text).ToString("#0.00")
            Rows(0).Item("CarNo") = Me.TBGiftNum.Text

        Else
            Exit Sub
        End If


    End Sub

    Private Sub txtmMoneyView_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmMoneyView.TextChanged
        Me.txtLitMoney.Text = Val(Me.txtSumMoneyView.Text) - Val(Me.txtmMoneyView.Text)
    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub

    Private Sub txtmMoney_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmMoney.TextChanged

    End Sub
End Class